poj 1318 Word Amalgamation
#include <iostream>
#include <string>
#include <set>
using namespace std;
bool match(string str1,string str2)
{
for(int i=0;i<str1.size();++i)
{
if(str2.find_first_of(str1[i])==string::npos)
return false;
else
str2.erase(str2.find_first_of(str1[i]),1);
}
return true;
}
int main()
{
string str,test;
bool tag;
set<string> dictionary;
set<string>::iterator pos;
while(cin>>str&&str!="XXXXXX")
{
dictionary.insert(str);
}
while(cin>>test&&test!="XXXXXX")
{
tag=0;
for(pos=dictionary.begin();pos!=dictionary.end();++pos)
{
if((*pos).size()!=test.size())
continue;
if(match(*pos,test))
tag=1,cout<<*pos<<endl;
}
if(!tag)
printf("NOT A VALID WORD\n");
printf("******\n");
}
return 0;
}