您的位置:首页 > 理论基础 > 计算机网络

Problem 1410 变位词 from http://acm.fzu.edu.cn/problem.php?pid=1410

2012-09-11 10:27 501 查看

Problem 1410 变位词

Accept: 546 Submit: 1584

Time Limit: 1000 mSec Memory Limit : 32768 KB



Problem Description

Mr. Right有一个奇怪的嗜好,就是看见一个单词就有找它所有的变位词的冲动。一个单词的变位词就是该单词所有字母的一个排列。



Input

输入数据第一行为一个整数n,1<=n<=10^5,之后n行每行只包含一个单词,不含词组。这些单词构成了Mr. Right的字典。每个单词长度不大于9个字母。接着一行为一个整数m,1<=m<=100,表示Mr. Right将看见的单词数。之后m行每行包含一个单词。(题目中出现的每个单词都只由小写字母组成)



Output

对应Mr. Right看到的每个单词,输出落在字典里的它的变位词的个数。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;

map<string, int> words;//如果用char *,key 为地址
int main(){

//freopen("in.txt", "r", stdin);
int n, m;
int count;
int i,j, times;
char a[10];
string str;
map<string, int>::iterator it;
while(cin>>n){
count = 0;
for(i=0;i<n;++i){
scanf(" %s", a);
sort(a, a+strlen(a));
str = a;//这里用str存储,然后用str作为key,省去每次的临时转换,大大减少消耗时间
it = words.find(str);
if(it == words.end()){
words[str] = 1;
}else{
times = it->second + 1;
words[str] = times;
}

}
cin>>m;
while(m--){
scanf(" %s", a);
sort(a, a+strlen(a));
str = a;
it = words.find(str);
if(it == words.end()){
printf("0\n");
}else{
printf("%d\n", it->second);
}
}
words.clear();
}

//fclose(stdin);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: