Codeforces Round #734 (Div. 3)-B1. Wonderful Coloring - 1

Posted Tisfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #734 (Div. 3)-B1. Wonderful Coloring - 1相关的知识,希望对你有一定的参考价值。

Codeforces Round #734 (Div. 3)-B1. Wonderful Coloring - 1

传送门
Time Limit: 1 second
Memory Limit: 256 megabytes

Problem Description

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 s s s 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:

E. g. consider a string s s s equal to “kzaaa”. One of the wonderful colorings of the string is shown in the figure.

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 k k k — the number of red (or green, these numbers are equal) letters in a wonderful coloring.

Input

The first line contains one integer t t t ( 1 ≤ t ≤ 1000 1 \\le t \\le 1000 1t1000) — the number of test cases. Then t t t test cases follow.

Each test case consists of one non-empty string s s s which consists of lowercase letters of the Latin alphabet. The number of characters in the string doesn’t exceed 50 50 50.

Output

For each test case, output a separate line containing one non-negative integer k k k — the number of letters which will be painted in red in a wonderful coloring.

Sample Input

5
kzaaa
codeforces
archive
y
xxxxxx

Sample Onput

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 3 3 3 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 5 5 5 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向下取整就好了。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        string str;
        cin>>str;
        int s=0;
        int a[26]={0}; // 26种字母分别出现了几次,初始值为0
        for(int i=0;i<str.size();i++)
        {
            a[str[i]-'a']++; // 这种字母的出现次数+1
            if(a[str[i]-'a']<=2)s++; // 如果这种字母是第一场出现或第二次出现,就可以涂色;出现3次以上的字母不再记录。
        }
        printf("%d\\n",s/2); // 因两种颜色使用次数必须相同,所以、2向下取整即可。
    }
    return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/119064494

以上是关于Codeforces Round #734 (Div. 3)-B1. Wonderful Coloring - 1的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #734 (Div. 3)-C. Interesting Story-题解

Codeforces Round #734 (Div. 3)-B1. Wonderful Coloring - 1

Codeforces Round #734 (Div. 3)-B2. Wonderful Coloring - 2-题解

Codeforces Round #734 (Div. 3)-B2. Wonderful Coloring - 2-题解

Codeforces Round #734 (Div. 3)-A. Polycarp and Coins-题解

Codeforces Round #734 (Div. 3)-A. Polycarp and Coins-题解