Poj 1035 spell checker (string)

Source: Internet
Author: User

Question:

Ask if the string you entered can be changed to a dictionary string by adding or subtracting one character or replacing one character.


Train of Thought Analysis:

Directly simulate the process of replacing, adding, and subtracting.

Compare the length of two strings. Simulation can be performed only when the difference is 1.

The simulation process is to perform matching one by one.

If the mismatch is found to be less than or equal to 1, the output can be made.


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <string>#include <map>#define maxn 10005using namespace std;char str[maxn][20];string tmp;map <string,bool>mymap;int main(){    while(scanf("%s",str[0])!=EOF)    {        mymap.clear();        tmp=str[0];        mymap[tmp]=true;        int i=1;        while(scanf("%s",str[i]) && str[i][0]!='#')        {            tmp=str[i];            mymap[tmp]=true;            i++;        }        char txt[20];        while(scanf("%s",txt) && txt[0]!='#')        {            tmp=txt;            if(mymap[tmp])printf("%s is correct\n",txt);            else            {                printf("%s:",txt);                int l=strlen(txt);                for(int j=0;j<i;j++)                {                    int len=strlen(str[j]);                    if(l+1==len)                    {                        int cnt=0;                        int p=0,k=0;                        while(p<l && k<len)                        {                            if(txt[p]==str[j][k]) p++,k++;                            else {                                cnt++;                                k++;                            }                        }                        if(cnt<=1 && p==l)printf(" %s",str[j]);                    }                    if(l==1+len)                    {                        int cnt=0;                        int p=0,k=0;                        while(p<len && k<l)                        {                            if(str[j][p]==txt[k])p++,k++;                            else {                                cnt++;                                k++;                            }                        }                        if(cnt<=1 && p==len)printf(" %s",str[j]);                    }                    if(l==len)                    {                        int cnt=0;                        for(int k=0;k<len;k++)if(txt[k]!=str[j][k])cnt++;                        if(cnt==1)printf(" %s",str[j]);                    }                }                puts("");            }        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.