这道模拟题有不少细节需要注意。如原来的串中的空行要忽略,最后每行80个字符,两个case之间要有空行等。还要注意不能直接模拟,会超时。可以先预处理一下,如2个R相当于没有没翻转。 程序代码: #include <iostream> #include <string> #include <cstdio> #include <algorithm> using namespace std; string Source, Code, Result; void Init() { Source.clear(); Code.clear(); Result.clear(); } int main() { int nCase; int n; int front; string input; //freopen("input.txt", "r", stdin); cin>>nCase; while(nCase--){ Init(); cin>>n; cin.ignore(); for(int i = 0; i < n; i++){ getline(cin, input); if(input.size()){ Source += input; } } while(cin>>input){ for(int i = 0; i < input.size(); i++){ if(input[i] == ' ') input.erase(i, 1); if(i && input[i] == 'R' && input[i - 1] == 'R') input.erase(i - 1, 2); } Code += input; if(input[input.size() - 1] == '#') break; } front = 0; for(int i = 0; i < Code.size() - 1; i++){ if(!Source.size()) break; if(Code[i] == 'R'){ if(!front) front = Source.size() - 1; else front = 0; }else if(Code[i] == 'G'){ int j = i; while(Code[j] == 'G' && (j - i) < Source.size()) j++; if(!front){ string s(Source, 0, j - i); Result += s; Source.erase(0, j - i); }else { string s(Source, Source.size() - (j - i), j - i), ss; reverse(s.begin(), s.end()); Result += s; Source.erase(Source.size() - 1, j - i); } i = j - 1; } } for(int i = 0; i < Result.size(); i++){ if(i && !(i % 80)) cout<<endl; cout<<Result[i]; } if(Result.size() && !(Result.size() % 80) && nCase){ cout<<endl; }else if(nCase) cout<<endl<<endl; } return 0; }