NOIP模拟(10.26)T1 抄代码

本文解析了一道NOIP模拟赛中的字符串匹配问题。通过模拟和暴力的方法验证两个字符串间是否存在合法的一一映射关系,并提供了详细的算法思路及C++实现代码。

抄代码

题目背景:

10.26 NOIP模拟T1

分析:模拟 + 暴力

 

在听了某大佬的分析之后,发现这个题,还是有神题的潜质的,然而,貌似出题人的本意是一道傻题······我们直接来看出题人的本意吧,本意就是,对于原串中的每一种小写字母,在新的串中如果都对应同一个小写字母,那么久说明是合法的否则不合法,只需要注意几点,一,要判断长度是否相等,二,对于不是小写字母的位置,必须完全相等,三,新串中的相同字母,在原串中不一定对应相同字母,然后暴力搞搞就好。

 

Source

/*
	created by scarlyw
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <ctime>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static bool iosig;
	static char c;
	for (iosig = false, c = read(); !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;
	}
	for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;

inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static bool iosig;
	static char c;
	for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;
	}
	for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 1000 + 10;
const int ALP = 26;

int n, len1, len2;
char s2[MAXN], s1[MAXN];
std::vector<int> pos[ALP];

inline bool not_alp(char c) {
	return (c > 'z' || c < 'a');
}

inline void solve() {
	R(n);
	while (n--) {
		for (int j = 0; j < ALP; ++j) pos[j].clear();
		len1 = len2 = 0;
		for (char c = 0; c != '\n' && c != -1; c = read()) s1[++len1] = c;
		for (char c = 0; c != '\n' && c != -1; c = read()) s2[++len2] = c;
		if (len1 != len2) {
			write_char('0'), write_char('\n');
			continue ;
		}
		bool flag = true;
		for (int i = 1; i <= len1; ++i)
			if (not_alp(s1[i]) || not_alp(s2[i]))
				if (s1[i] != s2[i]) {
					write_char('0'), write_char('\n'), flag = false;
					break ;
				}
		if (flag == false) continue ;
		for (int i = 1; i <= len1; ++i)
			if (!not_alp(s1[i])) pos[s1[i] - 'a'].push_back(i);
		for (int i = 0; i < ALP && flag; ++i) {
			if (!pos[i].size()) continue ;
			int st = pos[i][0];
			for (int p = pos[i].size() - 1; p > 0; --p)
				if (s2[pos[i][p]] != s2[st]) {
					write_char('0'), write_char('\n');
					flag = false;
					break ;
				}
		}
		if (flag) write_char('1'), write_char('\n');
	}
}

int main() {
//	freopen("copycat.in", "r", stdin);
//	freopen("copycat.out", "w", stdout);
	solve();
	flush();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值