*【CodeForces - 1150D】Three Religions(dp,预处理,思维)

题干:

During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now wonder if the followers of each religion could coexist in peace.

The Word of Universe is a long word containing the lowercase English characters only. At each moment of time, each of the religion beliefs could be described by a word consisting of lowercase English characters.

The three religions can coexist in peace if their descriptions form disjoint subsequences of the Word of Universe. More formally, one can paint some of the characters of the Word of Universe in three colors: 11, 22, 33, so that each character is painted in at most one color, and the description of the ii-th religion can be constructed from the Word of Universe by removing all characters that aren't painted in color ii.

The religions however evolve. In the beginning, each religion description is empty. Every once in a while, either a character is appended to the end of the description of a single religion, or the last character is dropped from the description. After each change, determine if the religions could coexist in peace.

Input

The first line of the input contains two integers n,qn,q (1≤n≤1000001≤n≤100000, 1≤q≤10001≤q≤1000) — the length of the Word of Universe and the number of religion evolutions, respectively. The following line contains the Word of Universe — a string of length nn consisting of lowercase English characters.

Each of the following line describes a single evolution and is in one of the following formats:

  • + ii cc (i∈{1,2,3}i∈{1,2,3}, c∈{a,b,…,z}c∈{a,b,…,z}: append the character cc to the end of ii-th religion description.
  • - ii (i∈{1,2,3}i∈{1,2,3}) – remove the last character from the ii-th religion description. You can assume that the pattern is non-empty.

You can assume that no religion will have description longer than 250250 characters.

Output

Write qq lines. The ii-th of them should be YES if the religions could coexist in peace after the ii-th evolution, or NO otherwise.

You can print each character in any case (either upper or lower).

Examples

Input

6 8
abdabc
+ 1 a
+ 1 d
+ 2 b
+ 2 c
+ 3 a
+ 3 b
+ 1 c
- 2

Output

YES
YES
YES
YES
YES
YES
NO
YES

Input

6 8
abbaab
+ 1 a
+ 2 a
+ 3 a
+ 1 b
+ 2 b
+ 3 b
- 1
+ 2 z

Output

YES
YES
YES
YES
YES
NO
YES
NO

Note

In the first example, after the 6th evolution the religion descriptions are: ad, bc, and ab. The following figure shows how these descriptions form three disjoint subsequences of the Word of Universe:

题目大意:

给一个1e5的串str,然后有三个起始空串,不超过1000次操作,对三个字符串的一个尾部加一个字符或者减一个字符,保证每个字符不会超过250

每次操作之后询问你这三个串是不是可以组成str的子序列,

比如ab,cd,ef可以组成acgbdef的子序列

解题报告:

[ i ][ j ] 代表的是在第j个位置之后的第i个字符的位置在哪里。

dp[ i ][ j ][ k ] 代表的是 匹配 第一个串前i个字符, 第二个串前j个字符, 第三个串前k个字符 这个状态时 最后面一个字符在原串的位置的最小值。

如果题目把三个串给你,那么就应该是个n^3的dp。

但是这题并没有这么善良,他的串是动态变化的。

比如,当操作为“+”的时候,如果添加的是s1,若s1的长度变为top[1]+1,可以发现,dp方程改变的只有dp [ top[1]+1] [ j ] [ k ] ,而其他的状态值都没有改变(因为只是在尾部操作,这点很关键)如果是s[2]也一样,就是dp [ i ] [ top[2] + 1 ] [ k ] 。

而当操作为"-"的时候,我们并不需要更新dp数组,还是假设操作对象是s1,我们只能用到dp [ top[1] - 1 ] [ j ] [ k ] ,而这之前已经处理好了,而假设我们下一次需要“+”,自然会覆盖dp [ top[1] ] [ j ] [ k ] 。所以不需要管。

对于某个字符下一次出现的位置,可以提前预处理出来。

所以总的复杂度为O(26n+250^2 * q)。

这题细节还是很多的。比如初始化的时候,需要初始化trie[n+1]  trie[n+2]这两维,因为后面更新的时候要用到。(当然 能否用到就取决于你设置的非法状态是多少,这里设置成n+1那么  代码汇总有可能用到 (n+1) + 1 这个状态的值,也就是n+2了,,所以也要对这一维进行初始化。)

其次不是所有erase操作都直接输出YES。因为万一我模板串是 'aa' ,然后我对1号串+了100次 ' a ',那我erase98次都应该输出NO才对。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1e5 + 5;
char ss[MAX];
int trie[MAX][28];
char s[4][MAX];
int top[4],up[4],down[4]; 
int dp[255][255][255];
int main()
{int n,q;memset(dp,0x3f,sizeof dp);dp[0][0][0]=0;cin>>n>>q;cin>>(ss+1);for(int i = 0; i<=26; i++) trie[n+1][i] = trie[n+2][i] = n+1;ss[0] = 0;for(int i = n; i>=0; i--) {int cur = ss[i] - 'a';for(int j = 0; j<26; j++) {if(j == cur) trie[i][j] = i;else trie[i][j] = trie[i+1][j];}}char op[5],tmp[5];int id;while(q--) {scanf("%s",op);if(op[0] == '+') {		scanf("%d",&id);scanf("%s",tmp);s[id][++top[id]] = tmp[0];for(int i = 1; i<=3; i++) down[i] = (i==id?top[i]:0),up[i] = top[i];for(int i = down[1]; i<=up[1]; i++) {for(int j = down[2]; j<=up[2]; j++) {for(int k = down[3]; k<=up[3]; k++) {dp[i][j][k] = n+1;if(i) dp[i][j][k] = min(dp[i][j][k],trie[dp[i-1][j][k]+1][s[1][i]-'a']);if(j) dp[i][j][k] = min(dp[i][j][k],trie[dp[i][j-1][k]+1][s[2][j]-'a']);if(k) dp[i][j][k] = min(dp[i][j][k],trie[dp[i][j][k-1]+1][s[3][k]-'a']);						}}}}else {scanf("%d",&id);top[id]--;}if(dp[top[1]][top[2]][top[3]] > n) puts("NO");else puts("YES");		}return 0 ;
}

给几个样例理解一下dp,对每次询问可以输出一下dp[top[1]][top[2]][top[3]]的值看看。

/*
6 5
eabbcd
+ 1 a
+ 1 b
+ 2 b
+ 2 c
+ 3 e

 

6 5
eabbcd

+ 3 e
+ 1 a
+ 1 b
+ 2 b
+ 2 c


6 6
aaaaaa
+ 1 a
+ 1 a
+ 2 a
+ 2 a
+ 2 a
+ 3 a
*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440382.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于android公交车线路查询论文文献,本科毕业论文---基于android的手机公交线路查询系统.doc...

毕 业 设 计( 论 文 )题目手机公交线路查询系统作者学院专业学号指导教师摘 要关键词&#xff1b;AbstractWith the level of people’s life improving,going out by bus become a necessary part of daily life.And the traffic line to destination should be known everyti…

【POJ - 3159】Candies (差分约束,卡SPFA)

题干&#xff1a; 在幼儿园的时候&#xff0c;Flymouse是班上的班长。有时班主任会给班上的孩子们带来一大袋糖果&#xff0c;让他们分发。所有的孩子都非常喜欢糖果&#xff0c;经常比较他们和别人买的糖果的数量。一个孩子A可以有这样的想法&#xff0c;尽管可能是另一个孩子…

计算机英语第六单元,计算机专业英语第六版第十单元课后汉译英,We do use other forms....这个do...

同样的 &#xff0c;多线程也存在许多缺点 &#xff0c;在考虑多线程时需要进行充分的考虑。多线程的主要缺点包括&#xff1a;Similarly, multi thread also has many shortcomings, in the consideration of the need for the full consideration of multiple threads. The m…

1.Hello,Python

本文为Kaggle Learn的Python课程的中文翻译&#xff0c;原文链接为&#xff1a;https://www.kaggle.com/colinmorris/hello-python 欢迎来到Kaggle Learn的Python课程。本课程将介绍在开始使用Python进行数据科学之前需要的基本Python技能。这些课程针对那些具有一些编程经验的…

【POJ - 1273】Drainage Ditches(网络流,最大流,模板)

题干&#xff1a; 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条水渠,给出这n条水渠所连接的点和所能流过的最大流量&#xff0c;求从源点到汇点能流过的最大流量。 Input 输入包括几种情况。 对于每种情况&#xff0c;第一行包含两个空格分隔的整数&#xff0c;N&a…

计算机网络阶段,计算机网络的发展大致可分为四个阶段,目前人类进入了()。 - 问答库...

问题&#xff1a;[单选] 计算机网络的发展大致可分为四个阶段&#xff0c;目前人类进入了()。A . 计算机网络阶段B . 信息高速公路阶段C . 计算机网络互联阶段D . 远程终端联机阶段教育心理学是研究教育情境中学生学习的基本心理规律的科学。王林平时成绩一般&#xff0c;但总想…

2.Functions and Getting Help

Functions and Getting Help 在本课中&#xff0c;我们将讨论函数&#xff1a;调用它们&#xff0c;定义它们&#xff0c;并使用Python的内置文档查找它们。 在某些语言中&#xff0c;定义函数必须要有特定的参数&#xff0c;每个参数都具有特定类型。 Python函数允许更灵活。…

华人科学家量子计算机,华人科学家在美国研发出性能强大的光子计算机,能够与中国的量子计算机一战高下!...

原标题&#xff1a;华人科学家在美国研发出性能强大的光子计算机&#xff0c;能够与中国的量子计算机一战高下&#xff01;在最近的《自然纳米技术》杂志上&#xff0c;一篇来自美国哥伦比亚大学的论文在业界掀起了轩然大波&#xff0c;一位名叫虞南方的物理学助理教授成功率领…

【BZOJ - 1001】狼抓兔子(无向图网络流,最小割,或平面图转对偶图求最短路SPFA)

题干&#xff1a; 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到&#xff0c;但抓兔子还是比较在行的&#xff0c; 而且现在的兔子还比较笨&#xff0c;它们只有两个窝&#xff0c;现在你做为狼王&#xff0c;面对下面这样一个网格的地形&#xff1a; …

3.Booleans and Conditionals

Booleans Python有bool类型数据&#xff0c;有两种取值&#xff1a;True 和 False. [1] x True print(x) print(type(x)) True <class bool> 我们通常从布尔运算符中获取布尔值&#xff0c;而不是直接在我们的代码中放入True或False。 这些是回答yes或no的运算符。…

能利用计算机来模拟某种真实的实验现象,自然现象或社会现象的课件是,《计算机辅助教学》课程复习资料...

考试资料《计算机辅助教学》课程复习资料一、单项选择题1. 教学软件又称为 A 。A.课件 B.多媒体 C.操作系统 D.应用软件2. 继课件之后的第二代教学软件称为 A 。A积件 B.课件 C.网络课件 D.智能型课件3. 建构主义学习理论强调认知主体是 B 。A. 教师 B. 学习者 C. 教务 D. 辅导…

【洛谷 - P3376 】【模板】网络最大流

题干&#xff1a; 如题&#xff0c;给出一个网络图&#xff0c;以及其源点和汇点&#xff0c;求出其网络最大流。 输入输出格式 输入格式&#xff1a; 第一行包含四个正整数N、M、S、T&#xff0c;分别表示点的个数、有向边的个数、源点序号、汇点序号。 接下来M行每行包含…

4.Lists

Lists Python中的列表表示有序的值。 可以使用方括号来定义&#xff0c;变量值之间用逗号隔开。 例如&#xff0c;这是几个素数的列表&#xff1a; [1] primes [2, 3, 5, 7] 我们可以将其他类型的东西放在列表中&#xff1a; [2] planets [Mercury, Venus, Earth, Mars…

怎么用计算机拟合数据,数据拟合的几个应用实例-毕业论文.doc

本科毕业设计(论文)数据拟合的几个使用实例燕 山 大 学年 月本科毕业设计(论文)数据拟合的几个使用实例学 院&#xff1a;专 业&#xff1a;学生姓名&#xff1a;学 号&#xff1a;指导教师&#xff1a;答辩日期&#xff1a;燕山大学毕业设计任务书学院&#xff1a; 系级教学单…

1.UNIX网络编程卷1:源码配置

本节主要介绍UNIX网络编程卷1&#xff08;第三版&#xff09;在Ubuntu16.04的配置问题&#xff0c;并运行一个简单时间获取客户程序。 1.首先下载源文件&#xff0c;链接如下&#xff1a;UNIX Network Programming Source Code 2.将下载好的压缩文件unpv13e.tar.gz解压&#…

【牛客 - 696C】小w的禁忌与小G的长诗(dp 或 推公式容斥)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/696/C 来源&#xff1a;牛客网 自从上次小w被奶牛踹了之后&#xff0c;就一直对此耿耿于怀。 于是"cow"成为了小w的禁忌&#xff0c;而长得和"cow"很像的"owc"…凡是…

html 地球大气,地球大气层为什么永远不会消失?

地球的大气层是一个很神奇的存在&#xff0c;几十亿年来&#xff0c;它就如同一层厚厚的保护膜&#xff0c;将地球与太空完全阻隔起来&#xff0c;正因为如此&#xff0c;地球上的生命才能够繁衍生息&#xff0c;代代相传。相信很多人都会有这样的疑问&#xff0c;为什么地球上…

5.Loops and List Comprehensions

Loops 循环是一种重复执行某些代码的方法。 [1] planets [Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune] for planet in planets:print(planet, end ) # print all on same line Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune 注意for循环…

【牛客 - 696D】小K的雕塑(dp,鸽巢原理,01背包类问题)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/696/D 来源&#xff1a;牛客网 小K有n个雕塑&#xff0c;每个雕塑上有一个整数 若集合T中的每一个元素在n个雕塑上都能找得到&#xff0c;则称这个集合为一个优秀的集合 小K想知道所有大小<k优秀…

计算机专业需要汇编语言,重点大学计算机专业系列教材·汇编语言程序设计

重点大学计算机专业系列教材汇编语言程序设计语音编辑锁定讨论上传视频本词条缺少概述图&#xff0c;补充相关内容使词条更完整&#xff0c;还能快速升级&#xff0c;赶紧来编辑吧&#xff01;《重点大学计算机专业系列教材汇编语言程序设计》是2009年10月1日清华大学出版社出版…