Time Limit: 2 Seconds Memory Limit: 65536 KB
Have you played Draw Something? It’s currently one of the hottest social drawing games on Apple iOS and Android Devices! In this game, you and your friend play in turn. You need to pick a word and draw a picture for this word. Then your friend will be asked what the word is, given the picture you have drawn. The following figure illustrates a typical scenario in guessing the word.
As you see, when guessing a word you will be given the picture and 12 letters. You must pick some of these letters to form a word that matches the picture. Each letter can only be used once. It is a lot of fun if your friend is a talented painter, but unfortunately some drawings from your friend are totally incomprehensible. After several times of becoming mad by the drawings, you find a way to cheat in the game.
In this game, letters not included in the correct answer are randomly generated. If you cannot find the correct answer when guessing, you can write down all the letters and restart the game. Then you would find some of these letters are changed. Of course these changed letters will never appear in the answer. By eliminating these letters you are a step closer to the answer.
So In this problem, you need to write a program to automate the cheating process. Given N strings of letters to the same picture, you need to eliminate as many letters as possible, and output the remaining letters.
Input
There are multiple test cases. The first line of the input is an integer T ≈ 1000 indicating the number of test cases.
Each test case begins with a positive integer N ≤ 20 indicating the number of times you have entered the game. Then N lines follow. Each line is a string of exactly 12 uppercase letters, indicating the candidate letters in one guess. It is guaranteed that the answer has at least 1 letter and has no more than 12 letters.
Output
For each test case, output the remaining letters in alphabet order after the process described above. One line for each test case.
Sample Input
2
2
ABCDEFGHIJKL
ABCDEFGHIJKL
2
SAWBCVUXDTPN
ZQTLFJYRCGAK
Sample Output
ABCDEFGHIJKL
ACT
题目链接:ZOJ-3603
题目大意:给出n个字符串,找出这n个字符串的共有的字符,按字典序输出。
题目思路:直接模拟,需要注意可能有重复字母出现
以下是代码:
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
string s[25];
int vis[30]; //记录字母是否出现以及次数
int main(){
int t;
cin >> t;
while(t--)
{
int n;
scanf("%d",&n);
cin >> s[0];
string ret = s[0];
for (int i = 1; i < n; i++)
{
cin >> s[i];
memset(vis,0,sizeof(vis));
for (int j = 0; j < s[i].size(); j++)
{
vis[s[i][j] - 'A'] ++;
}
string temp;
for (int j = 0; j < ret.size(); j++)
{
if (vis[ret[j] - 'A'])
{
temp += ret[j];
vis[ret[j] - 'A']--;
}
}
ret = temp;
}
sort(ret.begin(),ret.end());
cout << ret << endl;
}
return 0;
}