题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <algorithm> #include <iostream> using namespace std; int main() { string s; int flag = 0; while (getline(cin,s)) { // 注意 while 处理多个 case for(int i = 0;i<s.size();i++){ if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){ flag = 0; continue; }else{ if(flag == 1){ s.erase(i,1); flag = 1; continue; }else{ s[i] = ' '; flag = 1; continue; } } } if(s[0]==' '){ s.erase(0,1); } reverse(s.begin(),s.end()); int rev_a = 0,rev_b = 0; char tem ; for(int i = 0;i<s.size();i++){ if(s[i]!= ' '){ rev_a = i; for(int j = i;j<=s.size();j++){ if(s[j] == ' '||j == s.size()){ rev_b = j; break; }else{ continue; } } for(int x = rev_a,y = rev_b-1; x<y; x++,y--){ s[x] ^= s[y]; s[y] ^= s[x]; s[x] ^= s[y]; } i = rev_b; }else{ continue; } } cout<<s<<endl; rev_a = rev_b = 0; flag = 0; s.clear(); } } // 64 位输出请用 printf("%lld")