首页 > 其他分享 >【luogu P5826】【模板】子序列自动机

【luogu P5826】【模板】子序列自动机

时间:2022-09-05 14:23:42浏览次数:97  
标签:字符 int luogu vector P5826 序列 now 模板

【模板】子序列自动机

题目链接:luogu P5826

题目大意

给你一个序列,每次再给你一个序列,问你新的序列是不是一开始的序列的子序列。

思路

如果字符少不难想象到一个 \(f_{i,j}\) 表示 \(i\) 这个位置开始第一个字符是 \(j\) 的位置,然后直接走看看行不行。
但是字符多,于是考虑用时间换空间。

考虑用 vector 记录每个字符在哪些位置出现。
那把每个 vector 排序一下之后你就可以通过二分(lower_bound)找到你当前位置下一个要的字符最近在哪里。

然后就好了。

代码

#include<cstdio>
#include<vector>

using namespace std;

const int N = 1e5 + 100;
const int M = 1e6 + 100;
int type, n, q, m, a[N];
int sz, b[M];
vector <int> f[N];

int main() {
	scanf("%d %d %d %d", &type, &n, &q, &m);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]), f[a[i]].push_back(i);
	
	while (q--) {
		scanf("%d", &sz); for (int i = 1; i <= sz; i++) scanf("%d", &b[i]);
		int now = 0;
		for (int i = 1; i <= sz && now <= n; i++) {
			vector <int>::iterator it = lower_bound(f[b[i]].begin(), f[b[i]].end(), now + 1);
			if (it == f[b[i]].end()) now = n + 1;
				else now = *it;
		}
		if (now <= n) printf("Yes\n");
			else printf("No\n");
	}
	
	return 0;
}

标签:字符,int,luogu,vector,P5826,序列,now,模板
From: https://www.cnblogs.com/Sakura-TJH/p/luogu_P5826.html

相关文章

  • 导出模板设置其中某一列下拉选
    导出模板设置其中某一列下拉选 *设置下拉选 */ for(inti=0;i<headers.length;i++){ Stringheader=headers[i]; if(header.equals("电站简称")){ Str......
  • 拿来即用的下载Excel模板
    模板导出拿来即用 @PostMapping("/templateExport") @ApiOperation(value="模板导出",notes="作者:yysd") publicReturnObjectexportAuditContent(HttpServletRe......
  • MLops:我最喜欢的数据科学项目的 Github 项目模板
    MLops:我最喜欢的数据科学项目的Github项目模板source:unsplash.com-@yancyminTLDR:在这个故事中,我将分享一个git项目结构,我经常将其用作数据科学项目的起点,并......
  • 【2022.9.2】Django框架(网页伪静态、视图层、模板层)
    学习内容概要网页伪静态视图层三板斧JsonResponseform表单上传文件FBV与CBV(核心)CBV源代码(面向对象)模板层模板语法传值模板语法之过滤器模板语法之标签......
  • 【luogu CF633H】Fibonacci-ish II(莫队)(线段树)(矩阵乘法)
    Fibonacci-ishII题目链接:luoguCF633H题目大意给你一个序列,每次问你一个区间,把里面的数拿出来去重排序,第i个位置乘上斐波那契数列第i项之后所有数的和。思路这题......
  • SPFA例题/模板
    https://www.acwing.com/problem/content/1131/ #include<bits/stdc++.h>#definefore(x,y,z)for(LLx=(y);x<=(z);x++)#defineforn(x,y,z)for(LLx=(y);x<(z);......
  • 算法模板
    基础算法倍增intget(intl,intr){intd=r-l+1;intc=upper_bound(one,one+max_v+1,d)-one-1;returnmax(dp[l][c],dp[r-one[c]......
  • django4/网页伪静态/视图层/模板层
    网页伪静态动态页动态网页,页面代码虽然没有变,但是显示的内容却是可以随着时间、环境或者数据库操作的结果而发生改变的。静态页即静态网页,是实际存在的,无需经过服务器......
  • enjoy模板引擎
    <dependency> <groupId>com.jfinal</groupId> <artifactId>enjoy</artifactId> <version>5.0.0</version></dependency>importcom.jfinal.kit.Kv;importcom.jfina......
  • velocity模板渲染引擎
    <dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>使用人数最多的版本</version></dependency>im......