poj 1035 字符串匹配

//刚开始傻乎乎的拿字典树写,佛了,就一水题

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1e4 + 15;
string strArr[maxn];
int cnt = 0;
int main()
{
    while(1)
    {
        cin>>strArr[cnt++];
        if(strArr[cnt-1]=="#")
        {
            --cnt;
            strArr[cnt] = "";
            break;
        }
    }//输入字典
    string words;
    while(cin>>words)
    {
        if(words=="#")
            break;
        int i;
        for(i=0;i!=cnt;++i)
            if(strArr[i]==words)
            {
                cout<<words<<" "<<"is correct"<<endl;
                break;
            }
        if(i!=cnt)
            continue;
        //比较缺多或替换了的
        cout<<words<<":";
        for(int i=0;i!=cnt;++i)
        {
            if(words.length()==strArr[i].length())//如果字符匹配长度相等,单纯替代
            {
                int differ = 0;
                for(int j=0;j!=words.length();++j)
                    if(words[j]==strArr[i][j])
                        ++differ;
                if(differ==words.length()-1)
                    cout<<" "<<strArr[i];
            }//单纯的取代一个字符
            if((words.length()-strArr[i].length())==1||(words.length()-strArr[i].length())==-1)
            {
                //用短的字符匹配长的字符
                string strShort,strLong;
                if(words.length()<strArr[i].length())
                {
                    strShort = words;
                    strLong = strArr[i];
                }else{
                    strShort = strArr[i];
                    strLong = words;
                }
                int cur = 0;//当前匹配的longstr len
                int num = 0;//匹配的正确字符个数
                for(int j=0;j<strShort.length();++j)
                {
                    for(int k=cur;k<strLong.length();++k)
                    {
                        if(strShort[j]==strLong[k])
                        {
                            cur = k+1;
                            ++num;
                            break;//进行下一个字符匹配
                        }
                    }
                }//短匹配长字符
                if(num==strShort.length())
                    cout<<" "<<strArr[i];
            }//如果存在增删的单词
        }
        cout<<endl;
    }
}
不怕万人阻挡,只怕自己投降。
原文地址:https://www.cnblogs.com/newstartCY/p/11522128.html