跳到正文

个人记录

子序列自动机

用户 1171351约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;

const int N = 1e6+10;
int lst[26], nxt[N][26];

/*
a b c c c(abcdefghij.. = -1)

*/

void work()
{
	string str;
	cin >> str;
	int n = str.size()-1;
	//维护子序列自动机
	memset(lst, -1, sizeof(lst));
	for(int i = n; i >= 0; i--)//倒序遍历位置
	{
		for(int j = 0; j < 26; j++)//遍历的元素、字母
		{
			nxt[i][j] = lst[j];
		}
		lst[str[i] - 'a'] = i;
	}
	int m;
	cin >> m;
	while(m--)
	{
		string s;
		cin >> s;
		int pos = lst[s[0] - 'a'];//s[0]
		bool flg = 1;
		for(int i = 1; i < s.size(); i++)
		{
			pos = nxt[pos][s[i]-'a'];
			if(pos == -1)
			{
				flg = 0;
				break;
			}
		}
		if(flg == 0) cout << "No\n";
		else cout << "Yes\n";
	}
}

int main()
{
	int _ = 1;
	// cin >> _;
	while(_--)
	{
		work();
	}
	return 0;
}
原文