1 class Solution 
 2 {
 3 public:
 4     string toGoatLatin(string S) 
 5     {
 6         S.push_back(' ');         //add a space that the loop can deal with the last word
 7         string a(150,'a');        // string to add 'a'
 8         unordered_set<char> vowel={'a','e','i','o','u','A','E','I','O','U'};
 9         int front=0;
10         int len=S.size();
11         int alen=1;
12         string res;
13         for(int i=0;i<len;i++)      //once we meet a space ,we can deal with the word before it
14         {
15             if(S[i]==' ')
16             {
17                 string cur=S.substr(front,i-front);
18                 if(vowel.find(cur[0])==vowel.end())      
19                 {
20                     cur.push_back(cur[0]);
21                     cur.erase(cur.begin());
22                 }        
23                 res=res+cur+"ma"+a.substr(0,alen)+" ";
24                 alen++;
25                 front=i+1;
26             }
27         }
28         res.pop_back();    //delete the extra space
29         return res;
30     }
31 };

把原字符串剪断,一个单词一个单词处理,再把单词拼接成结果即可

posted on 2018-06-16 15:13  高数考了59  阅读(220)  评论(0)    收藏  举报