Anagrams (II)
One of the preferred kinds of entertainment of people living in final stages of XX century is filling in the crosswords. Almost every newspaper and magazine has a column dedicated to entertainment but only amateurs have enough after solving one crossword. Real professionals require more than one crossword for a week. And it is so dull - just crosswords and crosswords - while so many other riddles are waiting out there. For those are special, dedicated magazines. There are also quite a few competitions to take part in, even reaching the level of World Championships. Anyway - a lot.Anagrams (II) |
You were taken on by such a professional for whom riddle solving competing is just a job. He had a brilliant idea to use a computer in work not just to play games. Somehow anagrams found themselves first in the line. You are to write a program which searches for anagrams of given words, using a given vocabulary, tediously filled with new words by yours employer.
Input
The first line of the input is an integer M, then a blank line followed by M datasets. There is a blank line between datasets. The structure of each dataset is given below:.............. ................ END
is an integer number N < 1000.
up to
are words from the vocabulary.
up to
are the words to find anagrams for. All words are lowercase (word END means end of data - it is NOT a test word). You can assume all words are not longer than 20 characters.Output
For each
list the found anagrams in the following way:Anagrams for:) ...............
should be printed on 3 chars.In case of failing to find any anagrams your output should look like this:
Anagrams for:Print a blank line between datasets.No anagrams for:
Sample Input
1 8 atol lato microphotographics rata rola tara tola pies tola kola aatr photomicrographics END
Sample Output
Anagrams for: tola 1) atol 2) lato 3) tola Anagrams for: kola No anagrams for: kola Anagrams for: aatr 1) rata 2) tara Anagrams for: photomicrographics 1) microphotographics
Miguel A. Revilla
2000-01-10
string anagrams
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0
using namespace std;
vector<string>seq,query;
struct Node{
Node(int a,string b):ind(a),s(b){}
int ind;
string s;
};
bool comp(Node a,Node b){return a.s<b.s;}
void getNum(){
int cnt[26];
string s;
vector<string>res[query.size()],qs;
vector<Node>sst;
vector<Node>::iterator ite,start;
rep(i,seq.size()){
string s2;
memset(cnt,0,sizeof(cnt));
s=seq[i];
rep(j,s.size())cnt[s[j]-'a']++;
rep(j,26){if(cnt[j])s2+=char(cnt[j]+'0');else s2+='0';}
sst.push_back(Node(i,s2));
}
rep(i,query.size()){
string s2;
memset(cnt,0,sizeof(cnt));
s=query[i];
rep(j,s.size())cnt[s[j]-'a']++;
rep(j,26){if(cnt[j])s2+=char(cnt[j]+'0');else s2+='0';}
if(DBG)printf("qs %s %s\n",query[i].c_str(),s2.c_str());
qs.push_back(s2);
}
sort(sst.begin(),sst.end(),comp);
rep(i,qs.size()){
start = lower_bound(sst.begin(),sst.end(),Node(0,qs[i]),comp);
for(ite=start;ite!=sst.end();ite++)
if(qs[i]==(*ite).s){
if(DBG)printf("%s: found %s\n",query[i].c_str(),seq[(*ite).ind].c_str());
res[i].push_back(seq[(*ite).ind]);
}
}
rep(i,query.size())
if(!res[i].size())printf("Anagrams for: %s\nNo anagrams for: %s\n",query[i].c_str(),query[i].c_str());
else{
printf("Anagrams for: %s\n",query[i].c_str());
rep(j,res[i].size())printf(" %d) %s\n",j+1,res[i][j].c_str());
}
}
int main(){
int a,b;
bool ll=0;
string s;
scanf("%d ",&a);
rep(i,a){
seq.clear(),query.clear();
cin>>b;
rep(i,b)cin>>s,seq.push_back(s);
while(cin>>s){
if(s=="END")break;
query.push_back(s);
}
if(ll)printf("\n");ll=1;
getNum();
}
}
沒有留言:
張貼留言