824. Goat Latin

本文介绍了一种特殊的语言转换算法——Goat Latin。该算法能够将输入的英文句子转换为一种特殊形式,遵循特定的语言转换规则,如添加额外的字符和调整单词结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.

We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)

The rules of Goat Latin are as follows:

  • If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
    For example, the word 'apple' becomes 'applema'.
     
  • If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".
    For example, the word "goat" becomes "oatgma".
     
  • Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
    For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.

Return the final sentence representing the conversion from S to Goat Latin. 

 

Example 1:

Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

Example 2:

Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

 

Notes:

  • S contains only uppercase, lowercase and spaces. Exactly one space between each word.
  • 1 <= S.length <= 150.

解题思路:将字符串分词,用到了istringstream 将字符串分词,判断这个词是否含有元音字母,用到了unordered_set存储元音字母(unordered_set不含重复元素),如果含有元音字母,将第一个字符放到最后。再加 ma,第一个词多加一个 a,第二个词多加两个 a
class Solution {
public:
    string toGoatLatin(string S) {
        unordered_set<char> w({'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'});
        int count=0,i;
        istringstream iss(S);
        string res, c;
        while(iss>>c){
            res+=' '+ (w.count(c[0])==1?c:c.substr(1)+c[0]) + "ma";
            for(i=0, ++count ; i<count;i++)res+='a';
        }
        return res.substr(1);
    }
};

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_STATES 16 // 总共有2^4=16种状态 // 定义状态结构 typedef struct { int farmer; int wolf; int goat; int cabbage; } State; // 定义队列节点结构 typedef struct { State state; int parent; int step; char action[50]; } QueueNode; // 定义队列 QueueNode queue[MAX_STATES]; int front = 0, rear = 0; // 检查状态是否安全 bool is_safe(State s) { if (s.farmer != s.goat && (s.goat == s.wolf || s.goat == s.cabbage)) { return false; } return true; } // 检查状态是否为目标状态 bool is_goal(State s) { return s.farmer == 1 && s.wolf == 1 && s.goat == 1 && s.cabbage == 1; } // 检查状态是否已经访问过 bool is_visited(State s, State visited[], int count) { for (int i = 0; i < count; i++) { if (visited[i].farmer == s.farmer && visited[i].wolf == s.wolf && visited[i].goat == s.goat && visited[i].cabbage == s.cabbage) { return true; } } return false; } // 打印步骤 void print_step(QueueNode node) { printf("步骤%d: %s\n", node.step, node.action); printf("状态: 农夫: %s, 狼: %s, 羊: %s, 白菜: %s\n", node.state.farmer ? "北岸" : "南岸", node.state.wolf ? "北岸" : "南岸", node.state.goat ? "北岸" : "南岸", node.state.cabbage ? "北岸" : "南岸"); } // 打印解决方案 void print_solution(int index) { if (index == -1) return; print_solution(queue[index].parent); print_step(queue[index]); } // 广度优先搜索 void bfs() { State visited[MAX_STATES]; int visited_count = 0; State initial_state = {0, 0, 0, 0}; queue[rear].state = initial_state; queue[rear].parent = -1; queue[rear].step = 0; sprintf(queue[rear].action, "初始状态"); rear++; visited[visited_count++] = initial_state; while (front < rear) { State current = queue[front].state; int current_index = front; int current_step = queue[front].step; front++; if (is_goal(current)) { printf("祝贺您! 过河问题求解成功!\n"); print_solution(current_index); return; } for (int i = 0; i < 4; i++) { State next_state = current; next_state.farmer = 1 - next_state.farmer; char action[50]; if (i == 0) { next_state.wolf = 1 - next_state.wolf; sprintf(action, "农夫把狼带到%s", next_state.farmer ? "北岸" : "南岸"); } else if (i == 1) { next_state.goat = 1 - next_state.goat; sprintf(action, "农夫把羊带到%s", next_state.farmer ? "北岸" : "南岸"); } else if (i == 2) { next_state.cabbage = 1 - next_state.cabbage; sprintf(action, "农夫把白菜带到%s", next_state.farmer ? "北岸" : "南岸"); } else { sprintf(action, "农夫独自回到%s", next_state.farmer ? "北岸" : "南岸"); } if (is_safe(next_state) && !is_visited(next_state, visited, visited_count)) { queue[rear].state = next_state; queue[rear].parent = current_index; queue[rear].step = current_step + 1; sprintf(queue[rear].action, "%s", action); rear++; visited[visited_count++] = next_state; } } } printf("未找到解决方案!\n"); } // 主函数 int main() { bfs(); // 开始广度优先搜索 return 0; }分析一下
最新发布
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值