hihocoder 字元消除 阿新 • • 發佈:2019-01-22 #include<iostream> #include<string> using namespace std; string delestr(string s){ string sc; for(int j=0;j<s.length()-1;j++){ if(s[j]!=s[j+1]){ if(j==0||s[j]!=s[j-1]){ sc+=s[j]; } } }if(s[s.length()-2]!=s[s.length()-1]){sc+=s[s.length()-1];} if(sc!=s && sc.length()>1){ sc=delestr(sc); } return sc; } int main(){ int strnum;int Max[100]; int maxscore=0,score; cin>>strnum; for(int i=0;i<strnum;i++){ string s,t,scopy;cin>>s; //while(cin>>s){ for(int k=0;k<s.length()+1;k++){ for(int m=0;m<3;m++){ t=s.substr(0,k); t+=('A'+m); t+=s.substr(k,s.length()-k); scopy=delestr(t); score=s.length()-scopy.length()+1; maxscore=(score>maxscore)?score:maxscore; } }Max[i]=maxscore;maxscore=0; //} }for(int w=0;w<strnum;w++)cout<<Max[w]<<endl;system("pause"); return 0; }