难受,在本地写得,忘记删除中间测试cout了,怎么都ac不了,大佬们看看我写的暴力法错了吗?ps:代码有点丑,望指正。
#include<bits/stdc++.h> using namespace std; int main(){ string s; cin>>s; int n = s.size(); int p = 0; int q = 0; int maxS = 1; string ans = ""; while(p < n){ while(p+q <= n-q){ if(s.substr(p,q) == s.substr(p+q,q)){ maxS = max(maxS,q); } q++; } int count = 1; while(p+count*maxS <= n-maxS &&s.substr(p,maxS) == s.substr(p+count*maxS,maxS)){ count++; } char t = count+'0'; ans=ans + t + "(" + s.substr(p,maxS) + ")"; p += count*maxS; q = 0; maxS = 1; } cout<<ans<<endl; return 0; }
全部评论
(1) 回帖