题目:http://hihocoder.com/problemset/problem/1039
思路:
【转】1. AC代码
#include<iostream>
using namespace std;
#include<string.h>
int main()
{
int t,i,j,k,v[105],f,minn,h,i1;
char c,s1[105],s3[105];
cin>>t;
string s2;
while(t--)
{ cin>>s2;
f=minn=999999;
h=s2.length();
string s3(s2);//把s2复制给s3。
for(j=0;j<3;j++)
{ c='A'+j;
for(i=0;i<=h;i++)
{
s2=s3;
s2.insert(s2.begin()+i,c);//在所有位置上插入一个字符
for(k=0;s2[k]!=0;k++)
s1[k]=s2[k];
s1[k]='\0';
while(1)
{memset(v,0,sizeof(v));
if(strstr(s1,"AA")==NULL&&strstr(s1,"BB")==NULL&&strstr(s1,"CC")==NULL)
break;
for(i1=0;i1<strlen(s1)-1;i1++)
if(s1[i1]==s1[i1+1])
v[i1]=v[i1+1]=1;
for(f=0,i1=0;i1<strlen(s1);i1++)
if(!v[i1])
s1[f++]=s1[i1];
s1[f]='\0';
}
if(minn>f)minn=f;
}
}
cout<<h-minn+1<<endl;
}
return 0;
}
【转】2. AC代码
#include <iostream>
#include<string>
#include<vector>
using namespace std;
string GetString(const string& str)
{
int len=str.size();
if(len<=1)
return str;
vector<char> v;
for(int i=0;i+1<len;++i)
{
if(str[i]==str[i+1])
{
while(i+1<len&&str[i]==str[i+1])
++i;
}
else
v.push_back(str[i]);
}
//最后一个元素
if(str[len-2]!=str[len-1])
v.push_back(str[len-1]);
return string(v.begin(),v.end());
}
int main()
{
int n;
string s;
cin>>n;
string Insert="ABC";
while(n--)
{
cin>>s;
int max=s.size();
//s=GetString(s);
int min=s.size();
/*
while(min>GetString(s).size())
{
s=GetString(s);
min=s.size();
}
*/
string test;
for(int i=0;i<s.size();++i)
{
for(int j=0;j<3;++j)
{
string str=s.substr(0,i+1)+Insert[j]+s.substr(i+1);
int len=str.size();
while(len>(str=GetString(str)).size())
{
len=str.size();
test=str;
}
if(min>str.size())
min=str.size();
}
}
cout<<max-min+1<<endl;
}
return 0;
}