PAT
PAT copied to clipboard
关于A1140最后一个测试点超时问题
感觉我的代码和您的代码区别不大,为何我最后一测试点超时了,而您的却没有,不懂 还有您的代码好像少了 stirng的头文件?
我的代码
#include <iostream> #include <string> using namespace std; int main() { string s,temp; int N; cin >> s>> N; for (int i = 2; i <= N; i++) { char c = s[0]; int count = 0; for (int j = 0; j < s.length(); j++) { if (s[j]==c) count++; else { temp = temp + to_string(c - '0') + to_string(count); c = s[j]; count = 1; } } temp = temp +to_string(c-'0') +to_string(count); s = temp; temp = ""; } cout << s; return 0; }
您的代码
#include <iostream> using namespace std; int main() { string s; int n, j; cin >> s >> n; for (int cnt = 1; cnt < n; cnt++) { string t; for (int i = 0; i < s.length(); i = j) { for (j = i; j < s.length() && s[j] == s[i]; j++); t += to_string((s[i] - '0')) + to_string(j - i); } s = t; } cout << s; return 0; }
string头文件不是必要的,在我的编译器里和OJ里面都可以通过