UVa 12820 – Cool Word
A "word" is defined as a string of lowercase letters.
A "cool word" is defined as a word that contains at least 2 different letters, and the occurrence count of each distinct letter is different.
Formally, a "cool word" is defined as follows:
Let w be a "word", S is the set of letters in w, and let f(c) represent the number of occurrences of the letter c in w (where c is within the range of set S).
A word w is considered a "cool word" if all f(c)'s are distinct.
For example:
"ada" is a "cool word" because f(a)=2, f(d)=1, and the letters are distinct.
"banana" is also a "cool word" because f(a)=3, f(n)=2, f(b)=1.
"bbacccd" is not a "cool word" because f(a)=f(d)=1.
Other examples of "cool words" include mammal, needed, papaya, referee, and senselessness.
Please read the list of words and count the number of "cool words."
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input contains multiple test cases. Each test case begins with an integer N (1 ≤ N ≤ 10000), representing the length of the list of words. The following N lines each contain one word, with each word containing at least 1 and at most 30 letters. | For each test case, output the test case number and the number of "cool words". |
2 ada bbacccd 2 illness a | Case 1: 1 Case 2: 0 |
Thought Process
Declare a Map MAP to record the occurrence count of each character.
Declare a Map<int, vector> ans and use a for loop to iterate through MAP. For each character, push it to ans according to its occurrence count.
Iterate through ans using a for loop. If there is any entry where it.second.size() > 1, it indicates that the word is not a cool word.
Also, it's important to note that if the string contains only one type of character, it doesn't qualify as a cool word.
Sample Code-ZeroJudge E706: Cool Word
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N, count = 1;
while (cin >> N)
{
int coolWord = 0;
for (int i = 0; i<N; i++)
{
string str;
cin >> str;
map<char, int>MAP;
for (int j = 0; j<str.length(); j++)
{
MAP[str[j]]++;
}
map<int, vector<char>>ans;
int word = 0;
for (auto it:MAP)
{
ans[it.second].push_back(it.first);
word++;
}
if (word == 1) continue;
bool cool = true;
for (auto it:ans)
{
if (it.second.size() > 1)
{
cool = false;
break;
}
}
if (cool) coolWord++;
}
cout << "Case " << count << ": " << coolWord << "\n";
count++;
}
}
//ZeroJudge E706
//Dr. SeanXD