This is a simplified version of the problem B2. Perhaps you should read the problem B2 before you start solving B1.
Paul and Mary have a favorite string ss which consists of lowercase letters of the Latin alphabet. They want to paint it using pieces of chalk of two colors: red and green. Let's call a coloring of a string wonderful if the following conditions are met:
- each letter of the string is either painted in exactly one color (red or green) or isn't painted;
- each two letters which are painted in the same color are different;
- the number of letters painted in red is equal to the number of letters painted in green;
- the number of painted letters of this coloring is maximum among all colorings of the string which meet the first three conditions.
E. g. consider a string ss equal to "kzaaa". One of the wonderful colorings of the string is shown in the figure.
The example of a wonderful coloring of the string "kzaaa".
Paul and Mary want to learn by themselves how to find a wonderful coloring of the string. But they are very young, so they need a hint. Help them find kk — the number of red (or green, these numbers are equal) letters in a wonderful coloring.
Input
The first line contains one integer tt (1 \le t \le 10001≤t≤1000) — the number of test cases. Then tt test cases follow.
Each test case consists of one non-empty string ss which consists of lowercase letters of the Latin alphabet. The number of characters in the string doesn't exceed 5050.
Output
For each test case, output a separate line containing one non-negative integer kk — the number of letters which will be painted in red in a wonderful coloring.
Sample 1
Inputcopy | Outputcopy |
---|---|
5 kzaaa codeforces archive y xxxxxx | 2 5 3 0 1 |
Note
The first test case contains the string from the statement. One of the wonderful colorings is shown in the figure. There's no wonderful coloring containing 33 or more red letters because the total number of painted symbols will exceed the string's length.
The string from the second test case can be painted as follows. Let's paint the first occurrence of each of the letters "c", "o", "e" in red and the second ones in green. Let's paint the letters "d", "f" in red and "r", "s" in green. So every letter will be painted in red or green, hence the answer better than 55 doesn't exist.
The third test case contains the string of distinct letters, so you can paint any set of characters in red, as long as the size of this set doesn't exceed half of the size of the string and is the maximum possible.
The fourth test case contains a single letter which cannot be painted in red because there will be no letter able to be painted in green.
The fifth test case contains a string of identical letters, so there's no way to paint more than one letter in red.
思路:重复的字母只算一次(一定也只会有一个红)+只出现了一次的字母/2向下取整(因为红色块需等于绿色块个数,一半红一半绿,若向上取整,红会比绿大一,不满足相等的条件)。
上网查了一下,发现一种更简洁的,算出出现次数<=2的字母个数/2(出现次数<=2的色块均可上色,非红即绿),直接出答案。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
//卡住的点:题意,不会统计字母个数
int t;
cin>>t;
while(t--)
{
int a[26]={0};
string s;
cin>>s;
int count=0;
int temp=0;
for (int i = 0; i < s.size(); i++)
{
a[s[i]-'a']++;//转为数字赋值为1
}
for (int i = 0; i < 26; i++)//要注意26这个范围,26是指26个字母,不能写其他!
{
if(a[i]==1)
temp++;
if(a[i]>=2)
count++;
}
cout<<count+temp/2<<endl;
}
}