ZCMU1633: 酷酷的单词
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 228 Solved: 119
[Submit][Status][Web Board]Description
输入一些仅由小写字母组成的单词。你的任务是统计有多少个单词是“酷”的,即每种字母出
现的次数都不同。
比如 ada 是酷的,因为 a 出现 2 次,d 出现 1 次,而 1 和 2 不同。再比如,banana 也是酷的,
因为 a 出现 3 次,n 出现 2 次,b 出现 1 次。但是,bbacccd 不是酷的,因为 a 和 d 出现的次数
相同(均为 1 次)。Input
输入包含不超过 30 组数据。每组数据第一行为单词个数 n (1<=n<=10000)。以下 n 行各包含一
个单词,字母个数为 1~30。Output
对于每组数据,输出测试点编号和酷单词的个数。
Sample Input
2
ada
bbacccd
2
illness
a
Sample Output
Case 1: 1
Case 2: 0
【分析】 Hash统计各个单词出现的次数。注意如果单词只有一个字母的话是不能作为酷单词的。
【代码】
#include<bits/stdc++.h>
using namespace std;
int a[200];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int n,ca=0;
while(~scanf("%d",&n))
{
int cnt=0;
for(int i=0;i<n;i++)
{
memset(a,0,sizeof(a));
string s;
cin>>s;
int len=s.length(),flag=1;
if(len==1)continue;
for(int j=0;j<len;j++)
{
a[s[j]]++;
}
sort(a,a+200,cmp);
for(int j=0;j<200;j++)
{
if(a[j]==0)break;
if(a[j]==a[j+1]&&a[j])
{
flag=0;break;
}
}
//cout<<"flag="<<flag<<endl;
if(flag)cnt++;
}
printf("Case %d: %d\n",++ca,cnt);
}
return 0;
}
ZCMU1002: Clairewd’s message
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 820 Solved: 316
[Submit][Status][Web Board]Description
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that each letter of these messages would be transfered to another one according to a conversion table.
Unfortunately, GFW(someone's name, not what you just think about) has detected their action. He also got their conversion table by some unknown methods before. Clairewd was so clever and vigilant that when she realized that somebody was monitoring their action, she just stopped transmitting messages.
But GFW knows that Clairewd would always firstly send the ciphertext and then plaintext(Note that they won't overlap each other). (但是GFW知道Clairewd总是首先发送密文然后发送明文(注意它们不会相互重叠))But he doesn't know how to separate the text because he has no idea about the whole message. However, he thinks that recovering the shortest possible text is not a hard task for you.
Now GFW will give you the intercepted text and the conversion table. You should help him work out this problem.
Input
The first line contains only one integer T, which is the number of test cases.
Each test case contains two lines. The first line of each test case is the conversion table S. S[i] is the ith latin letter's cryptographic letter. The second line is the intercepted text which has n letters that you should recover. It is possible that the text is complete.
T<= 100 ;
n<= 100000;Output
For each test case, output one line contains the shorest possible complete text.
Sample Input
2
abcdefghijklmnopqrstuvwxyz
abcdab
qwertyuiopasdfghjklzxcvbnm
qwertabcde
Sample Output
abcdabcd
qwertabcde
【题意】 第一行给出字母转换表,第二行的为截取的字符串。截取的字符串包含密文和明文(明文则是根据前面的密文转换的)。注意红字部分,密文是完整的,但明文不一定完整。所以要找出这个分界线。
【分析】哈希存储转换表。因为密文肯定是完整的,所以,我们可以从截取的串的一半长度开始枚举,看能否和转换表对应以及能否增加密文长度。分界线的话,则是该字母能对应,但下一个字母不能对应。注意,s的长度如果是奇数,要取(len/2)+1;
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1e6+5;
char s[maxn],table1[30],table2[30];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",table1,s);
int i;
for(i=0;i<26;i++)
table2[table1[i]-'a']=i;
int len=strlen(s);
if(len&1)i=(len>>1)+1;
else i=len>>1;
int k=0;
for(;i<len;i++)
{
if(s[k]!=table1[s[i]-'a'])
continue;
else
{
int flag=0;
k++;
for(int j=i+1;j<len;j++)
if(s[k++]!=table1[s[j]-'a'])
{
flag=1;break;
}
if(!flag)break;
}
}
for(int j=0;j<i;j++)
printf("%c",s[j]);
for(int j=0;j<i;j++)
printf("%c",table2[s[j]-'a']+'a');
printf("\n");
}
}