思维训练 之 hdu 4545

本文介绍了一种基于字符串的匹配算法,该算法通过遍历和字符转换实现对两个字符串的匹配判断。文章详细解释了如何利用字符映射表进行扩展字符匹配,并提供了完整的C++代码实现。
//  [5/4/2014 Sjm]
/*
遍历,匹配即可。
注意:一个字符可以转换成多个字符。
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
string str1, str2;
vector<char> myReplace[26];

void Solve()
{
	int len1 = str1.size(), len2 = str2.size();
	int tlen = 0;
	for (int i = 0; i < len2; i++) {
		if (str2[i] == str1[tlen]) tlen++;
		else {
			for (int j = myReplace[str2[i] - 'a'].size() - 1; j >= 0; j--) {
				if (str1[tlen] == myReplace[str2[i] - 'a'][j]) {
					tlen++;
					break;
				}
			}
			
		}
		if (tlen == len1) {
			printf("happy\n");
			return;
		}
	}
	printf("unhappy\n");
}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	myReplace[2].push_back('#');
	int T;
	scanf("%d", &T);
	for (int t = 1; t <= T; t++)
	{
		memset(myReplace, 0, sizeof(myReplace));
		printf("Case #%d: ", t);
		cin >> str1 >> str2;
		int n;
		scanf("%d", &n);
		while (n--) {
			char c1, c2;
			getchar();
			scanf("%c %c", &c1, &c2);
			//cin >> c1 >> c2;
			myReplace[c1 - 'a'].push_back(c2);
		}
		Solve();
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值