Created
March 23, 2015 06:40
-
-
Save HaiyangXu/86fe534fc3baaa26470d to your computer and use it in GitHub Desktop.
#1039 : 字符消除
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
using namespace std; | |
int reduce(string &str){ | |
if(str.empty())return 0; | |
int pos=0; | |
bool keep=true; | |
for(int i=0;i<str.size()-1;i++){ | |
if(str[i]==str[i+1]){ | |
keep=false; | |
} | |
else{ | |
if(keep)str[pos++]=str[i]; | |
keep=true; | |
} | |
} | |
if(keep)str[pos++]=str[str.size()-1]; | |
if(str.size()==pos)return pos; | |
str.resize(pos); | |
return reduce(str); | |
} | |
int main(){ | |
int nums=0; | |
cin>>nums; | |
while(nums--){ | |
string str; | |
cin>>str; | |
int maximum=1; | |
//int before=str.size()-reduce(str);//remove duplicate | |
//cout<<before<<" "; | |
char arr[]="ABC"; | |
for(int i=0;i<str.size();i++){ | |
for(int j=0;j<3;j++){ | |
string strdup(str); | |
strdup.insert(i,1,arr[j]); | |
int num=strdup.size()-reduce(strdup); | |
maximum=max(maximum,num); | |
} | |
} | |
//cout<<maximum+before<<endl; | |
cout<<maximum<<endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment