poj1035 Spell checker

字符串的基本题,暴力也可以过。

第一个#之前是字典中的字符串,第二个#之前是要check的字符串。

逐个讨论,相等的话就直接输出correct。

字符串长度相同或相差一的就看是否只有一个字符不同。

#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
char dic[10001][16],check[51][16];
int main()
{
    //freopen("test.txt","r",stdin);
    int flag=0;
    char temp[16];
    int N=0,M=0;
    int i,j;
    while(flag!=2)
    {
        scanf("%s",temp);
        if(temp[0]=='#')
        {
            flag++;
            continue;
        }
        if(flag==0)
        {
            strcpy(dic[N],temp);
            N++;
        }
        else
        {
            strcpy(check[M],temp);
            M++;
        }
    }
    bool correct;
    char obj[10001][16];
    int num,u,v,t;
    for(i=0;i<M;i++)
    {
        correct=false;
        num=0;
        for(j=0;j<N;j++)
        {
            int clen=strlen(check[i]),dlen=strlen(dic[j]);
            if(strcmp(check[i],dic[j])==0)
            {
                correct=true;
                break;
            }
            if(clen==dlen)
            {
                u=0,v=0,t=0;
                while(u<clen)
                {
                    if(check[i][u]!=dic[j][v])
                    t++;
                    u++;
                    v++;
                }
                if(t>=2)
                continue;
                strcpy(obj[num],dic[j]);
                num++;
                continue;
            }
            if(clen-dlen==1)
            {
                u=0,v=0,t=0;
                while(u<clen&&v<dlen)
                {
                    if(t==2)
                    break;
                    if(check[i][u]==dic[j][v])
                    {
                        u++;
                        v++;
                    }
                    else
                    {
                        u++;
                        t++;
                    }
                }
                if(t!=2)
                {
                    strcpy(obj[num],dic[j]);
                    num++;
                }
            }
            if(dlen-clen==1)
            {
                int u=0,v=0,t=0;
                while(u<clen&&v<dlen)
                {
                    if(t==2)
                    break;
                    if(check[i][u]==dic[j][v])
                    {
                        u++;
                        v++;
                    }
                    else
                    {
                        v++;
                        t++;
                    }
                }
                if(t!=2)
                {
                    strcpy(obj[num],dic[j]);
                    num++;
                }
            }
        }
        if(correct)
        printf("%s is correct\n",check[i]);
        else
        {
            printf("%s:",check[i]);
            for(j=0;j<num;j++)
            printf(" %s",obj[j]);
            printf("\n");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/longlongagocsu/p/2880379.html