*【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,一经查实,立即删除!

相关文章

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

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

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

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

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

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

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

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

【牛客 - 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日清华大学出版社出版…

计算机原理期中考试,计算机组成原理期中考试试题

一、单选题(每小题2分&#xff0c;共34分)1&#xff0e;完整的计算机系统应包括__________。A&#xff0e;运算器、存储器、控制器 B&#xff0e; 主机和实用程序C&#xff0e;配套的硬件设备和软件系统 D&#xff0e; 外部设备和主机2&#xff0e;下列数中真值最小的数是_____…

量子计算机的体积有多大,量子计算机也能实现摩尔定律

原标题&#xff1a;量子计算机也能实现摩尔定律量子计算机拥有很强大的计算力&#xff0c;但是这对IBM来说&#xff0c;似乎还不够。据CNET消息&#xff0c;IBM制作了一个路线图&#xff0c;表达出了自己在量子计算领域的野心。IBM在图表的纵轴上列出了一个单位“量子体积(Quan…

1.How Models work

Introduction 我们首先概述机器学习模型如何工作以及如何使用它们。如果您之前已完成统计建模或机器学习&#xff0c;这可能会感觉很基础。别担心&#xff0c;我们很快就会建立强大的模型。 本课程将为您构建以下场景的模型&#xff1a; 你的堂兄已经花了数百万美元预测房地产…

【POJ - 3020】Antenna Placement (匈牙利算法,二分图最小边覆盖)

题干&#xff1a; The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It …

【POJ - 2195】Going Home(二分图最优匹配,费用流 或 KM)

题干&#xff1a; On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step h…

微机原理实验8254计算机钢琴,GitHub - SincereXIA/PianoMFC: 西电微机原理课设项目,键盘电子乐器演奏程序设计(电子琴),MFC...

PianoMFC西电微机原理课设项目&#xff0c;键盘电子乐器演奏程序设计(电子琴)&#xff0c;MFC需要连接西电微机原理实验室提供的 QTH9054 微机试验箱&#xff0c;使用其蜂鸣器发声&#xff0c;若不连接&#xff0c;程序会直接播放 mp3 文件模拟钢琴声。请在 release 处下载编译…

5.Underfitting and Overfitting

在这一步结束时&#xff0c;您将了解欠拟合和过拟合的概念&#xff0c;并且您将能够应用这些办法来使您的模型更准确。 Experimenting With Different Models 现在您已经有了一种可靠的方法来测量模型精度&#xff0c;您可以尝试使用其他模型&#xff0c;并查看哪种模型可以提…

福建省计算机初级职称,2019福建助理工程师职称评定条件材料及审核管理制度...

一学历、资历条件要求(破格申报不在此列&#xff0c;详情请咨询了解)申报工程技术系列中级工程师须符合下列条件之一&#xff1a;1.博士研究生毕业&#xff1b;2.硕士研究生毕业后&#xff0c;从事所申报专业工作满3年&#xff1b;3.本科毕业后&#xff0c;从事所申报专业工作满…

【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)

题干&#xff1a; Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings…

打开电脑计算机超级慢,手把手教你电脑开机慢怎么办

等到花都谢了&#xff0c;你怎么还不开机&#xff1f;这电脑开机真是离奇的慢&#xff0c;有心将它换了&#xff0c;奈何兜里空空。凑合着用又无法忍受这种煎熬。其实你只需要用鼠标点几下就可以不用等待这漫长的开机过程了。高铁&#xff0c;飞机&#xff0c;网络&#xff0c;…

【POJ - 1486】Sorting Slides(思维建图,二分图求必须边,关建边,图论)

题干&#xff1a; Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not a very tidy person and has put all his transparencies on one big heap. Before giving the talk, he has to sort the slides. Being a kind of minima…

8.Using Categorical Data with One Hot Encoding

本教程是机器学习系列的一部分。 在此步骤中&#xff0c;您将了解“分类”变量是什么&#xff0c;以及处理此类数据的最常用方法。 Introduction 分类数据是仅采用有限数量值的数据。 例如&#xff0c;如果人们回答一项关于他们拥有哪种品牌汽车的调查&#xff0c;结果将是明…

iPhone换屏幕测试软件,怎样检验iPhone是否更换过屏幕?

原标题&#xff1a;怎样检验iPhone是否更换过屏幕&#xff1f;关注下图公众号&#xff0c;鉴定苹果手机真假↓↓↓购买新手机时&#xff0c;到手后会想手机各零部件是否是正品原装&#xff0c;就好比屏幕是否原装屏&#xff01;入手一部iPhone新机的时候&#xff0c;该如何检验…

《TCP/IP详解》学习笔记(一):基本概念

为什么会有TCP/IP协议 在世界上各地&#xff0c;各种各样的电脑运行着各自不同的操作系统为大家服务&#xff0c;这些电脑在表达同一种信息的时候所使用的方法是千差万别。就好像圣经中上帝打乱 了各地人的口音&#xff0c;让他们无法合作一样。计算机使用者意识到&#xff0c;…