思路:
首先可以得到两个信息一个是尽量匹配前面相同的字符,其次简化问题只关注相同的字符,所以只需要维护每个位置及该位置之后的26个字母最近的位置,就可以以o(1)的时间找到目标字符
代码:
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <bitset>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 1e6;
int nex[maxn + 9][40];
int last[40];
int main() {
string s;
cin >> s;
int len = s.size();
memset(last, -1, sizeof(last));
for (int i = len - 1; i >= 0; i--)//需要维护每个位置及该位置之后的26个字母最近的位置
{
last[s[i] - 'a'] = i;
for (int j = 0; j < 26; j++)
{
nex[i][j] = last[j];
}
}
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
string temp;
cin >> temp;
int l = 0;//在该位置及之后的位置的字符都可选
bool flage = 1;//判断是不是子序列
for (int j = 0; j < temp.size(); j++)
{
if (nex[l][temp[j] - 'a'] <l)//最近的位置比可允许的位置小
{
flage = 0;
break;
}
else
{
l = nex[l][temp[j] - 'a'] + 1;
}
}
if (flage)printf("Yes\n");
else printf("No\n");
}
}
// 64 位输出请用 printf("%lld")