这道题搞得还真不容易,一个=跟==,搞了自己近两个钟头。。。
用的是暴搜解决
/* Poj 1035, by Dream 2011/4/28 */
#include <iostream>
using namespace std;
char dic[10010][20];
unsigned int totalNo = 0;
void DealWord(const char *word);
int CalculateValue(const char* word1, const char*word2);
int main()
{
freopen("input.txt","r",stdin);
char tmpword[20];
totalNo = 0;
while(EOF != scanf("%s",tmpword))
{
if ('#' == tmpword[0])
{
break;
}
strcpy(dic[totalNo], tmpword);
++totalNo;
}
while(EOF != scanf("%s", tmpword))
{
if ('#' == tmpword[0])
{
break;
}
DealWord(tmpword);
}
return 0;
}
int CalculateValue(const char* word1, const char*word2)
{
int m = strlen(word1);
int n = strlen(word2);
int i = 0;
/* replace one word */
if (m == n)
{
while((i < m) && (word1[i] == word2[i]))
++i;
while(++i < m)
{
if (word1[i] != word2[i])
{
return 0;
}
}
}
else if (m == n + 1) //这里写成了if(m=n+1),悲剧
{
while((i < n) && (word1[i] == word2[i]))
++i;
while(++i < m)
{
if (word1[i] != word2[i-1])
{
return 0;
}
}
}
else if (m + 1 == n)
{
while((i < m) && (word1[i] == word2[i]))
++i;
while(++i < n)
{
if (word1[i - 1] != word2[i])
{
return 0;
}
}
}
else
{
return 0;
}
return 1;
}
void DealWord(const char *word)
{
unsigned int res = 0;
unsigned int crntMin = 10000;
bool bFound = false;
for (unsigned int i = 0; i < totalNo; ++i)
{
if (!strcmp(word, dic[i]))
{
bFound = true;
}
}
if (bFound)
{
printf("%s is correct/n", word);
return;
}
else
{
printf("%s:", word);
}
for (unsigned int i = 0; i < totalNo; ++i)
{
if (CalculateValue(word, dic[i]))
{
printf(" %s", dic[i]);
}
}
printf("/n");
}