POJ 1035 字符串

题目大意:给定一个字典,然后给定一组单词集合,如果该单词在字典中,则输出correct;否则,如果该单词能够通过1>插入一个字母 2>删除一个字母 3>替换一个字母三种操作中的一种转化为字典序中的某个单词,输出该单词。直接暴力过的,c++能过,g++超时。

//11222230	c00h00g	1035	Accepted	1200K	500MS	C++	1530B	2013-01-30 15:07:29
#include<string>
#include<iostream>
#include<map>
using namespace std;
string word[10005];
map<string,int> mp;
//判断单词能否替换 
bool check_replace(string a,string b,int len){
    int res=0;
    for(int i=0;i<len;i++)
        if(a[i]!=b[i]) res++;
    if(res==1) return true;
    return false;
}
//判断能否通过插入或者删除,如果插入,将长度小的字符串写在前面 
bool check_insert(string a,string b,int len1,int len2){
    int i=0,j=0;
    while(i<len1&&j<len2&&a[i]==b[j]){i++;j++;}
    j++;//b向后移动一个位置 
    while(i<len1&&j<len2&&a[i]==b[j]){i++;j++;}
    if(i==len1&&j==len2) return true;
    return false; 
}
int main(){
    int i=0;
    while(cin>>word[i]&&word[i]!="#"){
        mp[word[i]]=1;
        i++;
    }
    int n=i;
    string test;
    while(cin>>test&&test!="#"){
        if(mp[test])
            cout<<test<<" is correct"<<endl;
        else{
           cout<<test<<":";
           int len1=test.length();
           for(int i=0;i<n;i++){
               int len2=word[i].length();
               if(len1==len2){
                   if(check_replace(test,word[i],len1))
                       cout<<" "<<word[i];
               }else if(len1+1==len2){//insert a char
                   if(check_insert(test,word[i],len1,len2))
                       cout<<" "<<word[i];
               }else if(len1-1==len2){//delete a char
                   if(check_insert(word[i],test,len2,len1))
                       cout<<" "<<word[i];
               }
           }
           cout<<endl;
        }        
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值