POJ 1035 Spell checker 字符串

#include <iostream>
using namespace std;
string dic[10010];
string che[60];
int dc, cc, flag;

void setFile () // 输入
{
	dc = cc = 0;
	do
	{
		dc++;
		cin >> dic[dc];
	} while ( dc <= 10000 && dic[dc][0] != '#' );
	do
	{
		cc++;
		cin >> che[cc];
} while ( cc <= 50 && che[cc][0] != '#' );}

void modify( int n )
{
	string res[500], tempStr;
	int i, j, t, cnt = 0;
	for ( i = 1; i < dc; i++ )
	{
		t = che[n].size() - dic[i].size();
		if ( t == 1 ) //当待查的词比字典中的词长1时,将字典中的词插入一个字母来与其比较。
		{
			for ( j = 0; j < che[n].size (); j++ )
			{
				if ( che[n][j] != dic[i][j] )
				{
					tempStr = dic[i];
					tempStr.insert ( j, 1, che[n][j] );
					if ( tempStr == che[n] )
						res[++cnt] = dic[i];elsebreak;
				}
			}
		}
		if ( t == -1 ) //当待查的词比字典中的词短1时,将待查的词插入一个字母来与字典中的词比较。 
		{
			for ( j = 0; j < dic[i].size (); j++ ) 
			{
				if ( che[n][j] != dic[i][j] )
				{
					tempStr = che[n];
					tempStr.insert ( j, 1, dic[i][j] );
					if ( tempStr == dic[i] )
						res[++cnt] = tempStr;
					else
						break;
				}
			}
		}
		if ( t == 0 ) //待查的词与字典中的词同长时,替换一个字母比较。
		{
			for ( j = 0; j < che[n].size(); j++ ) 
			{
				if ( che[n][j] != dic[i][j] )
				{
					tempStr = che[n];
					tempStr.replace ( j, 1, dic[i], j, 1 );
					if ( tempStr == dic[i] )
						res[++cnt] = tempStr;elsebreak;
				}
			}
		}
	}
	for ( i = 1; i <= cnt; i++ )
		cout << ' ' << res[i]; cout << endl;
}

void check ()
{
	int i,j;
	flag = false;
	for ( i = 1; i < cc; i++ )
	{
		for ( j = 1; j < dc; j++ )
		{
			if ( che[i] == dic[j] )
			{
				flag = true;break;
			}
		}
		if ( flag )
			cout << che[i] << ' ' << "is correct" << endl;
		else
		{
			cout << che[i] << ":";
			modify( i );
		}
		flag = false;
	}
}

int main()
{
	setFile();
	check();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值