/*
* poj 1035 spell checker
* antsmall
* 10-09-30
* http://poj.org/problem?id=1035
*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Word{
char*data;
int seq;
Word(char*d, int s) {
data = d;
seq = s;
}
};
const int MAX_WORD_LEN = 15;
vector<Word*> words[MAX_WORD_LEN+1];
typedef vector<Word*>::iterator veciter;
// 用于排序比较两个字符串,按升序排列
inline bool lessCompStr(const Word*word1, const Word*word2) {
return strcmp(word1->data, word2->data) < 0;
}
// 用来排序word对象,按seq升序排列
inline bool lessCompWord(const Word*word1, const Word*word2) {
return word1->seq < word2->seq;
}
// 在单词表中查找值等于str的,有则true,无则false
bool findSameWord(char str[]) {
int strLen = strlen(str);
int compResult = 0;
for(veciter i = words[strLen].begin(); i != words[strLen].end(); i++) {
compResult = strcmp(str, (*i)->data);
// 结果==0,则相等,返回true
if(compResult == 0)
return true;
// 结果<0, 而且单词表里是按升序排的,那之后都会比str大,所以立即返回false
else if(compResult < 0)
return false;
}
return false;
}
int main()
{
char input[MAX_WORD_LEN+1];
int inputLen = 0;
int seq = 0;
// 单词表输入
while(cin>>input && strcmp(input, "#") != 0) {
inputLen = strlen(input);
char* tmp = new char(inputLen + 1);
strcpy(tmp, input);
words[inputLen].push_back(new Word(tmp, seq));
++seq;
}
// 单词表按字母升序排序
for(int i = 1; i <= MAX_WORD_LEN; i++) {
sort(words[i].begin(), words[i].end(), lessCompStr);
}
vector<Word*> foundWords;
// 要检查的单词输入
while(cin>>input && strcmp(input, "#") != 0) {
foundWords.clear();
// 先查找是否有匹配的,有则打印出来,continue
if(findSameWord(input)) {
printf("%s is correct\n", input);
continue;
}
// 输出单词
printf("%s:", input);
inputLen = strlen(input);
// 查找删除一个字母的
if(inputLen > 1) {
int j = 0, k = 0;
int diff = 0;
for(veciter i = words[inputLen-1].begin(); i != words[inputLen-1].end(); i++) {
j = k = diff = 0;
while (j < inputLen && k < inputLen-1) {
if(input[j] == (*i)->data[k]) {
++j; ++k;
} else {
++j;
++diff;
}
if(diff > 1)
break;
}
if(diff <= 1)
foundWords.push_back(*i);
}
}
// 查找修改一个字母的
{
int j = 0, k = 0;
int diff = 0;
for(veciter i = words[inputLen].begin(); i != words[inputLen].end(); i++) {
j = k = diff = 0;
while (j < inputLen && k < inputLen) {
if((*i)->data[j] != input[k])
++diff;
++j; ++k;
if(diff > 1)
break;
}
if(diff <= 1)
foundWords.push_back(*i);
}
}
// 查找添加一个字母的
if(inputLen < MAX_WORD_LEN) {
int j = 0, k = 0;
int diff = 0;
for(veciter i = words[inputLen+1].begin(); i != words[inputLen+1].end(); i++) {
j = k = diff = 0;
while (j < inputLen+1 && k < inputLen) {
if((*i)->data[j] == input[k]) {
++j; ++k;
} else {
++j;
++diff;
}
if(diff > 1)
break;
}
if(diff <= 1)
foundWords.push_back(*i);
}
}
if(foundWords.size() > 0) {
sort(foundWords.begin(), foundWords.end(), lessCompWord);
for (veciter i = foundWords.begin(); i < foundWords.end(); i++) {
printf(" %s", (*i)->data);
}
}
printf("\n");
}
//system("pause");
return 0;
}