题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1318
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
#include
<
set
>
#include < iostream >
#include < string >
#include < algorithm >
using namespace std;
int main()
{
set < string > dict;
string word;
char ch[ 10 ];
while ( true )
{
gets(ch);
if (strcmp(ch, " XXXXXX " ) == 0 ) break ;
dict.insert(ch);
}
while ( true )
{
gets(ch);
if (strcmp(ch, " XXXXXX " ) == 0 ) break ;
int count = 0 ;
word = ch;
sort(word.begin(),word.end());
do
{
if (dict.find(word) != dict.end())
{
cout << word.c_str() << endl;
count ++ ;
}
} while (next_permutation(word.begin(),word.end()));
if (count == 0 )
{
cout << " NOTAVALIDWORD " << endl;
}
cout << " ****** " << endl;
}
return 0 ;
}
#include < iostream >
#include < string >
#include < algorithm >
using namespace std;
int main()
{
set < string > dict;
string word;
char ch[ 10 ];
while ( true )
{
gets(ch);
if (strcmp(ch, " XXXXXX " ) == 0 ) break ;
dict.insert(ch);
}
while ( true )
{
gets(ch);
if (strcmp(ch, " XXXXXX " ) == 0 ) break ;
int count = 0 ;
word = ch;
sort(word.begin(),word.end());
do
{
if (dict.find(word) != dict.end())
{
cout << word.c_str() << endl;
count ++ ;
}
} while (next_permutation(word.begin(),word.end()));
if (count == 0 )
{
cout << " NOTAVALIDWORD " << endl;
}
cout << " ****** " << endl;
}
return 0 ;
}
本文提供了一段解决PKU ACM 1318问题的C++代码,该代码使用了标准模板库(set)来实现字典功能,并通过全排列算法找出所有可能的单词组合,最后对比字典进行匹配。
124

被折叠的 条评论
为什么被折叠?



