Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

帮助后来人_水题

Posted by witstorm at 2018-01-23 16:43:20 on Problem 1035
请注意规则,1、删除一个字母;2、替换一个字母;3、插入一个字母
从中可以得出如下结论:1、欲纠错单词和字典内单词长度之差绝对值小于2;2、没有2,有的只是怎么减少时间,我写的代码仍然可以继续优化,懒得优化大概两百多ms
假设 len 是欲纠错单词temp长度;dic_len是字典单词dic_word长度
case 1:len == dic_len ----》判断是否有temp == dic_word
case 2:len == dic_len 单case1不成立,---》replace一个字母
case 3:len = dic_len - 1 -----》insert一个字母
case 4:len= dic_len + 1 -----》delete一个字母
具体附代码

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>

char dictionary[10001][16];
int dictionary_count;
int dic_len[10001];
int word_len[16][10001];
int word_len_count[16];
char temp[16];
char temp_copy[16];
int main()
{
	dictionary_count = 0;
	int len;
	int lens;
	int flag;
	while (scanf("%s", dictionary + dictionary_count) && dictionary[dictionary_count][0] != '#')//input
	{
		len = strlen(dictionary[dictionary_count]);
		dic_len[dictionary_count] = len;
		//len = strlen(dictionary[dictionary_count]);
		word_len[len][word_len_count[len]] = dictionary_count;
		word_len_count[len]++;
		dictionary_count++;
	}
	while (scanf("%s", temp) && temp[0] != '#')
	{
		lens = strlen(temp);
		//len - 1;
		//if (len == 1)
		//{
		//	for (int i = 0; i < word_len_count[len]; ++i)
		//	{

		//	}
		//}
		flag = false;
		for (int i = 0; i < word_len_count[lens]; ++i)
		{
			if (strcmp(temp, dictionary[word_len[lens][i]]) == 0)
			{
				printf("%s is correct\n", temp);
				flag = true;
				break;
			}
		}
		if (flag) continue;
		printf("%s:", temp);
		for (int i = 0; i < dictionary_count; ++i)
		{
			len = lens;
			strcpy(temp_copy, temp);
			if (len == dic_len[i])//replace
			{
				int t = 0;
				for (int j = 0; j < len; ++j)
				{
					if (temp_copy[j] != dictionary[i][j])
					{
						t++;
					}
				}
				if (t == 1)
				{
					printf(" %s", dictionary[i]);
				}
			}
			else if (len == dic_len[i] + 1) //delete
			{
				int t;
				for (t = 0; t < len; ++t)
				{
					if (temp_copy[t] != dictionary[i][t])
					{
						break;
					}
				}
				for (; t < len; ++t)//del
				{
					temp_copy[t] = temp_copy[t + 1];
				}
				len--;
				for (t = 0; t < len ; ++t)
				{
					if (temp_copy[t] != dictionary[i][t])
					{
						break;
					}
				}
				if (t == len)
				{
					printf(" %s", dictionary[i]);
				}
			}
			else if (len == dic_len[i] - 1)//insert
			{
				int t;
				for (t = 0; t < dic_len[i]; ++t)
				{
					if (temp_copy[t] != dictionary[i][t])
					{
						break;
					}
				}
				for (int j = len; j >= t; --j)
				{
					temp_copy[j + 1] = temp_copy[j];
				}
				temp_copy[t] = dictionary[i][t];
				len++;
				for (; t < len; ++t)
				{
					if (temp_copy[t] != dictionary[i][t])
					{
						break;
					}
				}
				if (t == len)
				{
					printf(" %s", dictionary[i]);
				}
			}
		}
		printf("\n");
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator