day4 POJ 1035 Spell checker

模拟题,没什么可说的,可以先读入字典,再将单词逐个读入输出,可以保证顺序,同时注意对字符数组的操作要细心


#include"iostream"
#include"string.h"
#include"cmath"
using namespace std;
char d[10001][16];
char w[16];

int ld=0,lw=0;
int dlen[10001];
char ed[5]="#";
void work(char* str,int l,int k)
{
    //cout<<l<<' '<<dlen[k]<<endl;
    if(l==dlen[k])
    {
            int cnt=0;
            for(int i=0;i<l;i++) if(str[i]==d[k][i]) cnt++;
            if(cnt==l-1) cout<<" "<<d[k];

    }
    else if(l==dlen[k]-1)
    {
        for(int i=0;i<dlen[k];i++)
        {
           char temp[16]="";
           char temp1[16]="";
           strncpy(temp,d[k],i);
           strncpy(temp1,d[k]+i+1,dlen[k]-i-1);
           strcat(temp,temp1);
           //cout<<temp<<endl;
           //cout<<str<<endl;
           if(strcmp(str,temp)==0) { cout<<" "<<d[k]; break;}

        }
    }
    else if(l==dlen[k]+1)
    {
        for(int i=0;i<l;i++)
        {
           char temp[16]="";
           char temp1[16]="";
           strncpy(temp,str,i);
           strncpy(temp1,str+i+1,l-i-1);
           strcat(temp,temp1);
           //cout<<temp<<endl;
          // cout<<d[k]<<endl;
           if(strcmp(d[k],temp)==0) { cout<<" "<<d[k]; break;}
        }
    }
}


int main()
{
    while(cin>>d[ld])
    {
        if(strcmp(d[ld],ed)==0) break;
        ld++;
    }
    for(int i=0;i<ld;i++) dlen[i]=strlen(d[i]);

    while(cin>>w)
    {
        int flag=0;
        if(strcmp(w,ed)==0) break;
        lw=strlen(w);
        //cout<<w;
        for(int i=0;i<ld;i++)
        {

            if(strcmp(w,d[i])==0) { cout<<w<<" is correct"<<endl; flag=1; break;}

        }
        if(flag==0)
        {
            cout<<w<<":";
        for(int i=0;i<ld;i++)
        {
            if(abs(lw-dlen[i])<=1)
            work(w,lw,i);
        }
        cout<<endl;
        }

    }





}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值