1090: 水晶链
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2 Solved: 2
[ Submit][ Status][ Web Board]
Description
你现在有一条水晶链,上面镶着N个珠子,每个珠子可能是红色(r),绿色(g)或者蓝色(b)。Indira有一种神奇的魔法,就是可以将相邻两个不同颜色的珠子合并起来,变成一个第三种颜色的珠子。例如,如果我们的项链是
r g r b
然后我们合并前两个珠子,于是链子就会变成
b r b
很显然,通过不同的合并方式,我们最终可以得到不同种类的新链。你的任务是帮Indira找到这其中最短的那一条的长度。
Input
测试数据第一行是一个整数T(T≤20),表示测试数据的组数。
每组测试数据只包括一个长度不超过105的字符串,表示给出的初始的链。字符串只会包含字符'r', 'g'和'b'。
Output
对于每组测试数据,输出我们能得到的最短链长。
Sample Input
2
bb
rgbb
Sample Output
2
1
HINT
Source
如果字母不全相同的话答案只可能是1或2,两个相同的用字母0表示,其它三个分别用1,2,3表示
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int ans(char ch)
{
if(ch == 'r') return 1;
else return ch == 'g' ? 2 : 3;
}
int main()
{
#ifdef xxz
freopen("in","r",stdin);
#endif // xxz
int T;
cin>>T;
while(T--)
{
string str;
cin>>str;
int len = str.length();
bool same = true;
for(int i = 1; i < len; i++)
{
same &= str[i] == str[i-1];
}
int sum = 0;
if(same) cout<<len<<endl;
else{
for(int i = 0; i<len; i++)
{
sum ^= ans(str[i]);
}
int answer = sum == 0 ? 2 : 1;
cout<<answer<<endl;
}
}
return 0;
}