將句子編碼為豬拉丁語


在這個問題中,我們將把句子轉換為豬拉丁語。我們可以將每個單詞的第一個字元附加到最後,並在其後附加“ay”。

我們將看到三種將給定句子轉換為豬拉丁語的方法。邏輯是我們可以將第一個字元附加到末尾,並將其從字串中刪除。之後,我們可以將“ay”附加到單詞。

問題陳述 – 我們給定一個包含多個單詞的字串 alpha。我們需要將字串編碼為豬拉丁語。

注意 – 豬拉丁語是一種單詞加密技術,它對單詞進行一次左旋轉,並在字串末尾附加“ay”。

示例

輸入

alpha = "Welcome to the tutorialspoint!";

輸出

elcomeWay otay hetay utorialspoint!tay

解釋 – 我們將字串的每個單詞轉換為豬拉丁語。

  • • Welcome -> elcomeWay

  • • to -> otay

  • • the -> hetay

  • • tutorialspoint! -> utorialspoint!tay

輸入

alpha = ‘How are you’

輸出

owHay reaay ouyay

解釋 – 字串被轉換為豬拉丁語。

方法 1

在這種方法中,我們將使用 substr() 方法獲取字串的特定單詞。之後,我們將把第一個字元和“ay”附加到字串的末尾。

演算法

步驟 1 – 初始化“res”變數以儲存結果字串。

步驟 2 – 開始迭代字串,並用“p”初始化“q”變數。

步驟 3 – 如果 p 大於字串長度,則中斷迴圈。

步驟 4 – 使用 while 迴圈遞增“p”的值,直到我們得到空格字元並且 p 小於字串長度。

步驟 5 – 使用 substr() 方法獲取從“q + 1”索引開始且長度等於“p – q – 1”的子字串。此外,將第一個字元和“ay”附加到末尾。

步驟 6 – 如果“res”字串的長度為 0,則將豬拉丁語附加到“res”。否則,將空格和豬拉丁語附加到結果。

步驟 7 – 返回“res”字串。

示例

#include <bits/stdc++.h>
using namespace std;

string encryptString(string alpha) {
    string res = "";
    // Traverse string
    for (int p = 0; p < alpha.length(); p++) {
        int q = p;
        // If p is greater than or equal to alpha length.
        if (p >= alpha.length())
            break;
        // Take a word from the string
        while (p < alpha.length() && alpha[p] != ' ')
            p++;
        // For first word
        if (res.length() == 0) {
            // Put the first character at last, and append 'ay'.
            res.append(alpha.substr(q + 1, p - q - 1) + alpha[q] + "ay");
        } else // for other words
        {
            res.append(" " + alpha.substr(q + 1, p - q - 1) + alpha[q] + "ay");
        }
    }
    return res;
}
int main() {
    string alpha = "Welcome to the tutorialspoint!";
    cout << "The Pig Latin encrypted string is - " << (encryptString(alpha));
    return 0;
}

輸出

The Pig Latin encrypted string is - elcomeWay otay hetay utorialspoint!tay

時間複雜度 – O(N*M),其中 N 是字串長度,M 是獲取子字串的最大單詞長度。

空間複雜度 – O(N) 用於儲存豬拉丁語。

方法 2

我們在這種方法中優化了第一種方法的程式碼。在這種方法中,我們將在遍歷字串時獲取給定字串的每個單詞,而無需使用 substr() 方法,並將每個單詞轉換為豬拉丁語。

演算法

步驟 1 – 初始化“temp”和“res”字串。

步驟 2 – 在遍歷字串時,如果我們得到字元,則將其附加到“temp”字串。

步驟 3 – 如果當前字元是空格,則訪問“temp”字串的第一個字元,並將其附加到自身。此外,將“ay”附加到“temp”字串。

步驟 4 – 使用 erase() 方法從“temp”字串中刪除第一個字元。

步驟 5 – 重新初始化“temp”字串。

步驟 6 – 處理字串的最後一個單詞。

步驟 7 – 返回“res”字串。

示例

#include <bits/stdc++.h>
using namespace std;

string encryptString(string alpha) {
    string res = "";
    string temp = "";
    // Traverse string
    for (int p = 0; p < alpha.length(); p++) {
        // If space is found
        if (alpha[p] == ' ') {
            // Encode to Pig Latine
            temp = temp + temp[0];
            // Remove the first character
            temp.erase(temp.begin());
            // Add 'ay' at the end
            temp = temp + "ay ";
            res += temp;
            temp = "";
        } else // For other characters
        {
            temp += alpha[p];
        }
    }
    // Handling the last word
    temp = temp + temp[0];
    temp.erase(temp.begin());
    temp = temp + "ay ";
    res += temp;
    return res;
}
int main() {
    string alpha = "Welcome to the tutorialspoint";
    cout << "The Pig Latin encrypted string is - " << (encryptString(alpha));
    return 0;
}

輸出

The Pig Latin encrypted string is - elcomeWay otay hetay utorialspointtay

時間複雜度 – O(N) 用於迭代字串。

空間複雜度 – O(N) 用於儲存豬拉丁語字串。

方法 3

在這種方法中,我們將使用空格作為分隔符來分割字串,以獲取字串的所有單詞。之後,我們將更新字串的每個單詞並將其附加到新字串。

演算法

步驟 1 – 初始化“pig”字串。

步驟 2 – 使用“stringstream”獲取單詞流。

步驟 3 – 開始遍歷字串流,並在每次迭代中獲取單個單詞。

步驟 4 – 使用 substr() 方法從單詞中刪除第一個字元,並將第一個字元附加到末尾。此外,最後附加“ay”。

步驟 5 – 將更新後的單詞附加到“pig”字串的末尾。

步驟 6 – 返回“pig”字串。

示例

#include <bits/stdc++.h>
using namespace std;

string encryptString(string alpha) {
    string pig = "";
    // Split the string by space
    stringstream words(alpha);
    string singleWord;
    // Get each words
    while (words >> singleWord) {
        singleWord = singleWord.substr(1) + singleWord[0] + "ay";
        pig += singleWord + " ";
    }
    return pig;
}
int main() {
    string alpha = "Welcome to the tutorialspoint";
    cout << "The Pig Latin encrypted string is - " << (encryptString(alpha));
    return 0;
}

輸出

The Pig Latin encrypted string is - elcomeWay otay hetay utorialspointtay

時間複雜度 – O(N) 用於遍歷字串的每個單詞。

空間複雜度 – O(N) 用於儲存結果字串。

我們學習了三種將句子轉換為豬拉丁語的方法。第一種方法使用 substr() 方法更新單詞,第二種方法使用臨時字串變數獲取單詞並更新它。第三種方法將字串拆分為單詞陣列並更新每個單詞。

更新於: 2023年8月25日

213 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.