POJ1035(字符串)

大意:判断一个字符串能否由字典中的字符串替换,删除或插入一个字符得到。

分析:水题

代码

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char dic[10005][17],ch[55][17];
int dic1[10005];
void rep(char s[])
{
	char s1[17];
	for (int i = 0; dic[i][0] != '#'; i++)
	{
		int num = 0;
		if (strlen(s) == strlen(dic[i]))
		{
			for (int j = 0; j < strlen(s); j++)
			{
				if (s[j] != dic[i][j])
					num++;
			}
			if (num == 1)
			{
				dic1[i] = 1;

			}
		}
		if (strlen(s) + 1 == strlen(dic[i]))
		{
			int n = 0, m = 0;
			while(m <strlen(dic[i]))
			{
				if (s[n] == dic[i][m])
				{
					n++; m++;
				}
				else
				{
					m++;
				}
				if (m - n > 1)
					break;
			}
			if (m - n == 1)
				dic1[i] = 1;
		}
		if(strlen(s) - 1 == strlen(dic[i]))
		{
			int n = 0, m = 0;
			while (m < strlen(s))
			{
				if (s[m] == dic[i][n])
				{
					n++; m++;
				}
				else
				{
					m++;
				}
				if (m - n > 1)
					break;
			}
			if (m - n == 1)
				dic1[i] = 1;
		}
	}
}
int main()
{
	//freopen("C:\\in.txt", "r", stdin);
	for (int i = 0;; i++)
	{
		scanf("%s", dic[i]);
		if (dic[i][0]=='#')
			break;
	}
	for (int i = 0;; i++)
	{
		scanf("%s", ch[i]);
		if (ch[i][0] == '#')
			break;
	}
	for (int i = 0; ch[i][0] != '#'; i++)
	{
		memset(dic1, 0, sizeof(dic1));
		int flag = 0;
		for (int j = 0; dic[j][0] != '#'; j++)
		{
			if (strcmp(ch[i],dic[j])==0)
			{
				flag = 1;
				break;
			}
		}
		if (flag == 1)
			printf("%s is correct\n", ch[i]); 
		else
		{
			printf("%s:", ch[i]);
			rep(ch[i]);
			for (int i = 0; dic[i][0] != '#'; i++)
			{
				if (dic1[i] == 1)
					printf(" %s", dic[i]);
			}
			printf("\n");
		}
	}
	return 0;
}


posted @ 2016-05-19 22:10  Nickqiao  阅读(486)  评论(0编辑  收藏  举报