1035:拼写检查

字符串处理
AC代码1:
参考:https://blog.csdn.net/u012126974/article/details/18056877

//字符串处理
#include <iostream>
#include <cstring>
using namespace std;

int lens[10001];
char dic[10001][16], str[51];

bool Find(int n, int m) {//判断单词是否相似
	int i, j;
	j = lens[n] - m;
	if (j == 1) {//多一个字符,跳过该字符
		for (i = 0; i < m && dic[n][i] == str[i]; i++);
		for (; i < m && dic[n][i + 1] == str[i]; i++);
		if (i == m)
			return true;
	}
	if (j == 0) {//有一个字符不同,跳过该字符
		for (i = 0; i < m && dic[n][i] == str[i]; i++);
		for (i++; i < m && dic[n][i] == str[i]; i++);
		if (i == m)
			return true;
	}
	if (j == -1) {//少一个字符,跳过该字符
		for (i = 0; i < lens[n] && dic[n][i] == str[i]; i++);
		for (; i < lens[n] && dic[n][i] == str[i+1]; i++);
		if (i == lens[n])
			return true;
	}
	return false;
}

int main() {
	int i, len, k = 0;
	
	while (cin >> dic[k] && dic[k][0] != '#') {
		lens[k] = strlen(dic[k]);
		k++;
	}
	while (cin >> str && str[0] != '#') {
		len = strlen(str);
		for (i = 0; i < k; i++) {//k个字典单词
			if (lens[i] == len && strcmp(dic[i], str) == 0) {
				cout << str << " is correct";
				break;
			}
		}
		if (i == k) {
			cout << str << ":";
			for (i = 0; i < k; i++) {
				if (Find(i, len) == true)
					cout << " " << dic[i];
			}
		}
		cout << endl;
	}
	return 0;
}

AC代码2(指针)
参考:https://blog.csdn.net/hongbudao/article/details/76606693

//字符串处理
#include <iostream>
#include <cstring>
using namespace std;

char dic[10005][18];//字典
char s[18];//要检查的单词
int n = 0;//字典单词个数

//指针
bool del(char* s1, char* s2) {
	int dif = 0;
	while (*s1) {
		if (*(s1++) != *s2) {
			if (++dif > 1)
				return false;
		}
		else {
			s2++;
		}
	}
	return true;
}

bool add(char* s1, char* s2) {
	int dif = 0;
	while (*s2) {
		if (*s1 != *(s2++)) {
			if (++dif > 1)
				return false;
		}
		else {
			s1++;
		}
	}
	return true;
}

bool replace(char* s1, char* s2) {
	int dif = 0;
	while (*s1) {
		if (*(s1++) != *(s2++)) {
			if (++dif > 1)
				return false;
		}
	}
	return true;
}

int main() {
	while (cin >> dic[n] && dic[n][0] != '#')
		n++;
	while (cin >> s && s[0] != '#') {//依次检查
		bool flag = false;
		for (int i = 0; i < n; i++) {//正确
			if (strcmp(dic[i], s) == 0) {
				printf("%s is correct\n", s);
				flag = true;
				break;
			}
		}
		if (flag) continue;
		printf("%s:", s);
		int len1 = strlen(s), len2;
		for (int i = 0; i < n; i++) {
			len2 = strlen(dic[i]);
			if (len2 - len1 == 1) {//字典单词比当前单词长1,
				if (del(dic[i], s))
					printf(" %s", dic[i]);
			}
			else if (len2 - len1 == -1) {
				if(add(dic[i],s))
					printf(" %s", dic[i]);
			}
			else if (len2 == len1) {
				if(replace(dic[i],s))
					printf(" %s", dic[i]);
			}
		}
		cout << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值