The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence

简介: The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence

题意:给你一个文本字符串P,n个字符串si,然后判断字符串si是不是文本串P的子序列

枚举给个字符最早出现的位置就可以了
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
char s[maxn];
int pos[maxn][30];
int dis[maxn];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for(int i = n; i >= 1; i--) {
    for(int j = 0; j <= 26; j++) {
      pos[i][j] = pos[i + 1][j];
    }
    pos[i][s[i] - 'a'] = i;
    dis[s[i] - 'a'] = i;
  }
  scanf("%d", &n);
  for(int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    int m = strlen(s + 1); 
    int start = dis[s[1] - 'a'];
    if(start == 0) {
      printf("NO\n");
      continue;
    }
    int flag = 1;
    for(int j = 2; j <= m; j++) {
      if(pos[start + 1][s[j] - 'a'] == 0) {
        flag = 0;
        break;
      }
      start = pos[start + 1][s[j] - 'a'];
    }
    if(flag) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  } 
  return 0;
}
相关文章
Leetcode 365. Water and Jug Problem
一句话理解题意:有容积为x和y升的俩水壶,能不能量出z升的水。 我刚开始看到这题,立马就想了下暴力搜索的可能性,但考虑了下数据大小,立马放弃这个暴力的想法,于是意识到肯定有比较简单的数学方法,其实我自己没想到,后来看还是看了别人的代码,很多博客都直接给出了解法, 但没介绍为什么能这么解。所以我决定解释下我自己的思路。
46 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
71 0
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
86 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
92 0
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
67 0
|
机器学习/深度学习
The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree
The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree
89 0
LeetCode 365. Water and Jug Problem
有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?
80 0
LeetCode 365. Water and Jug Problem
|
人工智能
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
114 0
|
机器学习/深度学习 人工智能 BI
The 15th Chinese Northeast Collegiate Programming Contest
The 15th Chinese Northeast Collegiate Programming Contest
142 0
The 15th Chinese Northeast Collegiate Programming Contest C. Vertex Deletion(树形dp)
The 15th Chinese Northeast Collegiate Programming Contest C. Vertex Deletion(树形dp)
110 0