poj 1035 Spell checker

Spell checker

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 14190

Accepted: 5226

题目链接:http://poj.org/problem?id=1035

Description

You, as a member of a development team for a new spellchecking program, are to write a module that will check the correctness ofgiven words using a known dictionary of all correct words in all their forms.
If the word is absent in the dictionary then it can be replaced by correctwords (from the dictionary) that can be obtained by one of the followingoperations:
?deleting of one letter from the word;
?replacing of one letter in the word with an arbitrary letter;
?inserting of one arbitrary letter into the word.
Your task is to write the program that will find all possible replacements fromthe dictionary for every given word.

Input

The first part of the input file contains all words fromthe dictionary. Each word occupies its own line. This part is finished by thesingle character '#' on a separate line. All words are different. There will beat most 10000 words in the dictionary.
The next part of the file contains all words that are to be checked. Each wordoccupies its own line. This part is also finished by the single character '#'on a separate line. There will be at most 50 words that are to be checked.
All words in the input file (words from the dictionary and words to be checked)consist only of small alphabetic characters and each one contains 15 charactersat most.

Output

Write to the output file exactly one line for everychecked word in the order of their appearance in the second part of the inputfile. If the word is correct (i.e. it exists in the dictionary) write themessage: " is correct". If the word is not correct then write thisword first, then write the character ':' (colon), and after a single spacewrite all its possible replacements, separated by spaces. The replacementsshould be written in the order of their appearance in the dictionary (in thefirst part of the input file). If there are no replacements for this word thenthe line feed should immediately follow the colon.

Sample Input

i

is

has

have

be

my

more

contest

me

too

if

award

#

me

aware

m

contest

hav

oo

or

i

fi

mre

#

Sample Output

me is correct

aware: award

m: i my me

contest is correct

hav: has have

oo: too

or:

i is correct

fi: i

mre: more me

Source

Northeastern Europe1998

题意:

         输入字符串,下面的进行匹配,如果下面的单词通过增加或者删除或者替换一个字母与上面的相同就输出正确的每一个字符串,如果直接相同就输出该字符串

解题思路:

         首先接收输入,存放在一个结构体中,先依据字符串的长度进行排序,在分别写三个函数来处理插入,删除,替换,如果三个中有任何符合的字符串,就放在一个临时的结构体中(方便最后输出),如果有任何一个操作返回值为true,那么证明有与之匹配的字符串,直接输出。

代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define maxn 20005
using namespace std;

struct trieStr
{
    char ch[17];//字符串
    int len;//字符串长度
	int id;//编号——最后排序要用
};

struct findIndex
{
	int star;
	int len;
};

trieStr trie[maxn];
trieStr dict[55];
trieStr temp[55];
int ind;
int count1,count2;//表示长度

bool cmp(trieStr a,trieStr b)
{
	if(a.len<b.len)
		return true;
	if(a.len==b.len && strcmp(a.ch,b.ch)==-1)
		return true;
	return false;
}
bool cmp1(trieStr a,trieStr b)
{
	if(a.id<b.id)
		return true;
	return false;
}

//在trie中找到长度为len的从i开始的长度为len
findIndex getIndex(int len)
{
	int i;
	int flag=0;
	findIndex index;
	index.star=-1;
	index.len=0;
	for(i=0;i<=count1;i++)
	{
		if(trie[i].len==len)
		{
			if(flag==0)
			{
				index.star=i;
				flag=1;
			}
			index.len++;
		}
	}
	return index;
}

bool insert(trieStr str)
{//找长度多1的
	int i,j,k;
	int num;
	findIndex index;
	index=getIndex(str.len+1);
	for(i=index.star;i<(index.star+index.len);i++)
	{
		num=0;
		for(j=0,k=0;j<=str.len;j++,k++)
		{
			//一一比对,只能相差一个错误
			if(trie[i].ch[k]!=str.ch[j])
			{
				num++;
				j--;
				if(num>1)
					break;
			}
		}
		if(num==0)
		{
			printf("%s is correct",str.ch);
			return true;
		}
		if(num==1)
		{
			temp[ind]=trie[i];
			ind++;
		}
	}

	return false;
}
bool deleteing(trieStr str)
{//找长度少1的
	int i,j,k;
	int num;
	findIndex index;
	if(str.len==1)//如果只有一个字符那么就不执行次操作,否则会多输出一个空格,为此Presentation Error了n次
		return false;
	index=getIndex(str.len-1);
	for(i=index.star;i<(index.star+index.len);i++)
	{
		num=0;
		for(j=0,k=0;j<=str.len;j++,k++)
		{
			//一一比对,只能相差一个错误
			if(trie[i].ch[k]!=str.ch[j])
			{
				num++;
				k--;
				if(num>1)
					break;
			}
		}
		if(num==0)
		{
			printf("%s is correct",str.ch);
			return true;
		}
		if(num==1)
		{
			temp[ind]=trie[i];
			ind++;
		}
	}
	return false;
}
bool replace(trieStr str)
{//找长度相等的

	int i,j;
	int num;
	findIndex index;
	index=getIndex(str.len);
	for(i=index.star;i<(index.star+index.len);i++)
	{
		num=0;
		for(j=0;j<=str.len;j++)
		{
			//一一比对,只能相差一个错误
			if(trie[i].ch[j]!=str.ch[j])
			{
				num++;
				if(num>1)
					break;
			}
		}
		if(num==0)
		{
			printf("%s is correct",str.ch);
			return true;
		}
		if(num==1)
		{
			temp[ind]=trie[i];
			ind++;
		}
	}
	return false;
}


//输入
void input()
{
    char c;
    int id=0,i=0;

    while(true)
    {
        scanf("%c",&c);
        if(c=='#')
            break;
        if(c=='\n')
        {
            trie[i].len=id;
			trie[i].id=i;
            i++;
            id=0;
        }
        else
        {
            trie[i].ch[id]=c;
            id++;
        }
    }
	count1=i;

    id=0;
    i=0;
	scanf("%c",&c);
    while(true)
    {
        scanf("%c",&c);
        if(c=='#')
            break;
        if(c=='\n')
        {
            dict[i].len=id;
			trie[i].id=i;
            i++;
            id=0;
        }
        else
        {
            dict[i].ch[id]=c;
            id++;
        }
    }
	count2=i;
}

int main()
{
	int i,j;
	input();
	sort(trie,trie+count1,cmp);
	for(i=0;i<count2;i++)
	{
		ind=0;
		memset(temp,0,sizeof(0));//要记得初始化
		if(!replace(dict[i]) && !insert(dict[i])  && !deleteing(dict[i]))
		{
			sort(temp,temp+ind,cmp1);//顺序不能乱
			printf("%s:",dict[i].ch);
			for(j=0;j<ind;j++)
			{
				printf(" ");
				printf("%s",temp[j].ch);
			}
		}
		printf("\n");
	}
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值