http://acm.pku.edu.cn/JudgeOnline/problem?id=2934

运行了100毫秒就错了,总是找不到原因,谢谢...

#include<algorithm>
#include<string>
#include<stdio.h>
using namespace std;
struct STRING
{
    char str[30];
    int len;
    int pos;
};
int thesame(char a[],char b[],int len_a)//len_a==len_b;
{
    int i,t;
    t=0;
    if(strcmp(a,b)==0)return 1;
    for(i=0;i<len_a;i++){
        if(a[i]==b[i])continue;
        t++;
        if(t>=2)return 0;
        if(i<len_a-1){
            if(a[i]==b[i+1]&&a[i+1]==b[i])
                i++;
        }
    }
    return 2;
}
int similar(char a[],char b[],int len_a, int len_b)//len_a>len_b;
{
    int i,t=0;
    for(i=0;i<len_a;i++){
        if(a[i]==b[i-t])continue;
            t++;
        if(t>=2)return 0;
    }
    return 1;
}
int order(STRING a,STRING b)
{
    return a.len < b.len;
}
STRING p[11000];
char a[11000][30];
int main()
{
    int i,n,t,rec,pos,m;
    char sch[30];
    int s_len;
    //scanf("%d",&n);
    while(scanf("%d",&n)!=EOF){
    for(i=0;i<n;i++){
        scanf("%s",a+i);
        strcpy(p[i].str,a[i]);
        p[i].pos=i;
        p[i].len=strlen(p[i].str);
    }
    sort(p,p+n,order);//按长度排序;
//    for(i=0;i<n;i++)
//        printf("%s %d %d\n",p[i].str,p[i].pos,p[i].len);
    scanf("%d",&m);
    while(m--){
        scanf("%s",sch);
        s_len=strlen(sch);
        t=0;
        pos=200000;
        for(i=0;i<n;i++){
            if(s_len>p[i].len+2)continue;
            if(s_len==p[i].len+1){
                rec=similar(sch,p[i].str,s_len,p[i].len);
                if(rec==1){
                    t=1;
                    pos=p[i].pos;
                }
            }
            if(s_len==p[i].len){
                rec=thesame(p[i].str,sch,s_len);
                if(rec==1){
                    t=2;
                    pos=p[i].pos;
                    break;
                }
                if(rec==2){
                    if(pos>p[i].pos){
                        t=1;
                        pos=p[i].pos;
                    }
                }
            }
            if(s_len==p[i].len-1){
                rec=similar(p[i].str,sch,p[i].len,s_len);
                if(rec==1){
                    if(pos>p[i].pos){
                        t=1;
                        pos=p[i].pos;
                    }
                }
            }
        }
        switch(t){
        case 0:printf("%s is unknown\n",sch);break;
        case 1:printf("%s is a misspelling of %s\n",sch,a[pos]);break;
        case 2:printf("%s is correct\n",sch);break;
        }
    }
    }
    return 0;
}