http://poj.org/problem?id=1035
题意:
给出一个字典。
给出一系列单词,让你在字典匹配
如果存在,输出 字符串 is correct
否则,看能否找到 以下三种近似单词:
1同长度,只有一个位置不同
2长度-1,其余相同
3长度+1,除了那个+1的字符,其余相同
输出所有的类似单词
否则 输出原单词+空行
官方参考代码真是思路清晰,简洁啊。。。。自己之前写的简直是@#¥%¥…………¥%#
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const int mod=1000000007;
int min(int a,int b)
{return a<b?a:b;}
int max(int a,int b)
{return a>b?a:b;}
char tm[10005][20];
int can_replace(char *in,char *orig,int len)
{
int i;
int len2=strlen(orig);
i=0;
if (len==len2)
{
while(i<len2&&in[i]==orig[i])
i++;
while(++i<len2)
if (in[i]!=orig[i]) return 0;
}
else if (len==len2-1)
{
while(i<len2&&in[i]==orig[i])
i++;
while(++i<len2)
if (in[i-1]!=orig[i]) return 0;
}
else if (len==len2+1)
{
while(i<len&&in[i]==orig[i])
i++;
while(++i<len)
if (in[i]!=orig[i-1]) return 0;
}
else return 0;
return 1;
}
int main()
{
// freopen( "F:\\duipai\\1.out","w",stdout );
char tmp[20];
int j,i;
int ok=0;
while(scanf("%s",tm[++ok])!=EOF)
{
if (strcmp(tm[ok],"#")==0)break;
}
ok--;
while(scanf("%s",tmp)!=EOF)
{
if (strcmp(tmp,"#")==0)break;
int len=strlen(tmp);
int flag=0;
for (i=1;i<=ok;i++)
{
if (strcmp(tmp,tm[i])==0)
{
flag=1;break;
}
}
if (flag)
printf("%s is correct\n",tmp);
else
{
printf("%s:",tmp);
for (j=1;j<=ok;j++)
{
if (can_replace(tmp,tm[j],len))
printf(" %s",tm[j]);
}
printf("\n");
}
}
return 0;
}