POJ1035——spell checker

正好最近被人问到一个数据结构的问题,难住了。所以决定来刷刷数据结构的题,从初级的开始,当回炉再造了。

题目大概意思:

作为一个拼写的checker,遵从如下几个规则——

(1)对待检查单词,删除一个字母后,能在字典中找到;

(2)对待检查单词,替换一个字母(任意字母替换)后,能在字典中找到;

(3)对待检查单词,插入一个任意字母后,能在字典中找到;

INPUT:

第一部分是字典,字典最多10000各单词,字典输入结束以‘#’号结束,每个单词占据一行

第二部分是要检查的单词,最多50个,也以’#‘号结束

每个单词最多15个字母构成

OUTPUT:

找到一样的,直接输出该单词+“is correct”

没找到的话,输出该单词+“:”+字典中相似的单词

这道题我WA了很多次,一开始始终没想通替换,删除,插入的核心,当时还想真在26的字母中找一个插入,然后进行匹配呢。犯2了!

我AC的思路是这样的:

把字典,和待检查的单词都放到数组里去[10001][16],[51][16];

对规则用3个函数来写;

不管删除还是其他操作,都只能对1个字母,所以只要找待检查单词和字典中单词大小的绝对值<=1的,然后进行规则操作;

对每一个待检查单词,都遍历一遍字典(很暴力);

#include<iostream>
#include<string>
using namespace std;

char dict[10001][16];
char wdchk[51][16];
int count_dict = 0;//字典计数器
int count_wdchk = 0;//待检查单次计数器

/*删除待检查单次中一个字母*/
bool del(char *wd, char *dict)
{
	int dif = 0;//计数器,当前不同字符多少个
	while (*wd)
	{
		if (*wd != *dict)
		{
			wd++;
			dif++;
			if (dif > 1)return false;
		}
		else
		{
			wd++;
			dict++;
		}
	}
	return true;
}

/*在待检查单词中插入一个字母*/
bool append(char *wd, char *dict)
{
	int dif = 0;
	while (*dict)
	{
		if (*wd != *dict)
		{
			dict++;
			dif++;
			if (dif > 1)return false;
		}
		else
		{
			dict++;
			wd++;
		}
	}
	return true;
}

/*对待检查单词替换任意一个字母*/
bool replace(char *wd, char *dict)
{
	int dif = 0;
	while (*wd)
	{
		if (*wd != *dict)
		{
			wd++;
			dict++;
			dif++;
			if (dif > 1)return false;
		}
		else
		{
			wd++;
			dict++;
		}
	}
	return true;
}

int main(void){

	while (cin >> dict[count_dict] && dict[count_dict++][0] != '#');
	while (cin >> wdchk[count_wdchk] && wdchk[count_wdchk++][0] != '#');
	count_dict--;//去除‘#’
	count_wdchk--;
	//输入结束测试
	/*cout << count_dict << ' ' << count_wdchk << endl;
	int temp = count_wdchk;
	while (temp--)
	{
		cout << wdchk[temp] << endl;
	}
	*/
	int* DictLen = new int[count_dict];  //记录计算字典中各个单词的长度
	for (int n = 0; n<count_dict; n++)
		DictLen[n] = strlen(dict[n]);

	/*for (int c = 0; c < sizeof(DictLen); c++)
		cout<< DictLen[c] << " ";*/

	for (int i = 0; i < count_wdchk; i++)
	{
		int lenwd = strlen(wdchk[i]);
		int *address = new int[count_dict];
		int position=0;//待输出字典的位置
		bool flag = false;//标记字典里是否有wdchk[i]
		for (int j = 0; j < count_dict; j++)//遍历字典
		{
			if (lenwd==DictLen[j])
			{
				if (!strcmp(wdchk[i], dict[j]))
				{
					flag = true;
					break;
				}
				else if (replace(wdchk[i], dict[j]))
				{
					address[position++] = j;
				}
			}
			else if (lenwd - DictLen[j] == 1)
			{
				if (del(wdchk[i], dict[j]))
					address[position++] = j;
			}
			else if (DictLen[j] - lenwd == 1)
			{
				if (append(wdchk[i], dict[j]))
					address[position++] = j;
			}
		}
		//output
		if (flag)
			cout << wdchk[i] << " is corrent" << endl;
		else
		{
			cout << wdchk[i] << ":";
			for (int k = 0; k < position; k++)
				cout <<' '<< dict[address[k]];
			cout << endl;
		}
	}
	return 0;
}
时间: 2024-07-31 06:30:20

POJ1035——spell checker的相关文章

POJ1035 Spell checker

在做用户查找时 因为要把查找的结果动态加载和显示,所以,那些html元素要由Ajax动态生成.用户打开查找界面时,有系统推荐的用户,而当用户按条件查找后,查找的结果动态加载和显示.所以考虑到用js来搞. 这个for循环就是移除已有的表单.然后根据Ajax请求过来的数据,动态生成新的表单对象.一定要注意j变量从大往小循环,否则,删除div元素后会引起serchResultLenth=serchResult.children.length;长度的变化(这个问题摸索了好久,才搞定,切记) for(va

POJ1035——Spell checker(字符串处理)

Spell checker DescriptionYou, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms. If the word is ab

poj1035(Spell checker)

题目地址:Spell checker 题目大意: 给你一个关于字符串的字典,让你根据字典里的字符串判断输入字符串的正确性.如果字符串不正确,可以通过以下的操作来输出字符串的可能性:1.可以替换一个字符,2.可以删除一个字符,3.可以添加一个字符.如果满足以上操作,说明都算是字符串的可能性,然后输出. 解题思路: 判断四种情况即可,如果正确直接输出,如果出现1.2.3这几种情况,先将字符串存到一个字符数组里s[N][N].如果全部判断完,没有正确性,输出可能性的字符串. 代码: 1 #includ

[ACM] POJ 1035 Spell checker (单词查找,删除替换增加任何一个字母)

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word

[ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word

Spell checker(暴力)

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20188   Accepted: 7404 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word

【POJ】1035 Spell checker

字典树. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <string> 7 using namespace std; 8 9 typedef struct Trie { 10 int in; 11 Trie *next[26]; 12 } Trie;

Spell checker POJ 1035 字符串

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25426   Accepted: 9300 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word

POJ 1035 Spell checker (串)

题目大意: 问你后面输入的串能不能通过  加减一个字符,或者替换一个字符变成字典中的串. 思路分析: 直接模拟替换加减的过程. 比较两个串的长度.要相差为1 的时候才能进行模拟. 模拟的过程就是进行一个个的匹配. 发现失配的次数小于等于 1就可以输出. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <string> #i