B. Suffix Structures

https://codeforces.com/problemset/problem/448/B

题意:给定字符串s和t,判断s能否通过纯交换字符位置,或者纯移除字符,或者移除+交换来获得字符串t。

总结:纯交换看字符数是否相等;纯移除使用双指针看t是否是s的子序列;交换+移除看s是否包含了t的序列并且s纯移除不能获得t;剩余情况就是need tree。

inline void solve() {
	string s, t;
	cin >> s >> t;

	int n = (int)s.size();
	int m = (int)t.size();

	array<int, 26> cnt = {};
	for (auto c : s) {
		cnt[c - 'a'] ++;
	}
	for (auto c : t) {
		cnt[c - 'a'] --;
	}

	bool ok = true;
	for (int i = 0; i < 26; ++i) {
		if (cnt[i] != 0) {
			ok = false;
			break;
		}
	}
	if (ok) {
		cout << "array\n";
		return;
	}

	ok = true;
	for (int i = 0; i < 26; ++i) {
		if (cnt[i] < 0) {
			ok = false;
			break;
		}
	}

	if (!ok) {
		cout << "need tree\n";
		return; 
	}

	int i, j;
	for (i = 0, j = 0; i < n && j < m;) {
		if (s[i] == t[j]) {
			i ++;
			j ++;
		}
		else {
			i++;
		}
	}

	if (j == m) {
		cout << "automaton\n";
	}
	else {
		cout << "both" << '\n';
	}
}
posted @ 2025-04-24 09:38  _Yxc  阅读(9)  评论(0)    收藏  举报