CodeForces - 1150 D Three Religions
题解:
id[ i ][ j ] 代表的是在第j个位置之后的第i个字符的位置在哪里。
dp[ i ][ j ][ k ] 代表的是 第一个串匹配到第i个位置, 第二个串匹配到第j个位置, 第三个串匹配到第k个位置之后,最后面一个字符的位置在哪里。
如果题目只询问一次,那么应该很容易想到n^3的写法。
for(int i = ; i <= n1; ++i){
for(int j = ; j <= n2; ++j){
for(int k = ; k <= n3; ++k){
if(i+j+k) dp[i][j][k] = n + ;
if(i) dp[i][j][k] = min(dp[i][j][k], id[ss[][i]-'a'][dp[i-][j][k]]);
if(j) dp[i][j][k] = min(dp[i][j][k], id[ss[][j]-'a'][dp[i][j-][k]]);
if(k) dp[i][j][k] = min(dp[i][j][k], id[ss[][k]-'a'][dp[i][j][k-]]);
}
}
但是,在一共有q次询问的前提下,肯定是不能每次询问都直接n^3的暴力得到的。
现在假如我们知道了 dp[3][4][6]的信息。
现在在第3个串后面加了一个字符,也就是说我们需要知道dp[3][4][7]的信息。
可以观察2遍的 n^3中的结果。
其中 for i from 0 to 3
for j from 0 to 4
for k from 0 to 6 的dp值和前面没有任何不同。
唯一区别的是 :
for i from 0 to 3
for j from 0 to 4
for k from 7 to 7 的dp值会发生变化。
所以我们只需要跑一边
for k from 7 to 7
for i from 0 to 3
for k from 0 to 4的dp值,更新这一块的dp值就好了。
如果 i + 1了 或者 j + 1了也是一样的道理。
所以每次更新所跑的值都是 len ^ 2。
最后的复杂度就是 q * len ^ 2. (len <= 250)。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
char s[N];
int id[][N];
int dp[][][];
char ss[][N];
void Ac(){
int n, m;
scanf("%d%d", &n, &m);
scanf("%s", s+);
for(int i = ; i < ; ++i)
id[i][n+] = n+;
for(int j = n; j >= ; --j){
for(int i = ; i < ; ++i){
id[i][j] = id[i][j+];
}
if(j < n) id[s[j+]-'a'][j] = j+;
}
int l1, l2, l3, n1, n2, n3;
l1 = l2 = l3 = n1 = n2 = n3 = ;
char op[];
int t;
for(int _ = ; _ <= m; ++_){
scanf("%s", op);
if(op[] == '+'){
scanf("%d%s", &t, op);
if(t == ) {
++n1; ss[][n1] = op[];
for(int i = n1; i <= n1; ++i){
for(int j = ; j <= l2; ++j){
for(int k = ; k <= l3; ++k){
if(i+j+k) dp[i][j][k] = n + ;
if(i) dp[i][j][k] = min(dp[i][j][k], id[ss[][i]-'a'][dp[i-][j][k]]);
if(j) dp[i][j][k] = min(dp[i][j][k], id[ss[][j]-'a'][dp[i][j-][k]]);
if(k) dp[i][j][k] = min(dp[i][j][k], id[ss[][k]-'a'][dp[i][j][k-]]);
}
}
}
}
if(t == ) {
++n2; ss[][n2] = op[];
for(int j = n2; j <= n2; ++j){
for(int i = ; i <= l1; ++i){
for(int k = ; k <= l3; ++k){
if(i+j+k) dp[i][j][k] = n + ;
if(i) dp[i][j][k] = min(dp[i][j][k], id[ss[][i]-'a'][dp[i-][j][k]]);
if(j) dp[i][j][k] = min(dp[i][j][k], id[ss[][j]-'a'][dp[i][j-][k]]);
if(k) dp[i][j][k] = min(dp[i][j][k], id[ss[][k]-'a'][dp[i][j][k-]]);
}
}
}
}
if(t == ) {
++n3; ss[][n3] = op[];
for(int k = n3; k <= n3; ++k){
for(int i = ; i <= l1; ++i){
for(int j = ; j <= l2; ++j){
if(i+j+k) dp[i][j][k] = n + ;
if(i) dp[i][j][k] = min(dp[i][j][k], id[ss[][i]-'a'][dp[i-][j][k]]);
if(j) dp[i][j][k] = min(dp[i][j][k], id[ss[][j]-'a'][dp[i][j-][k]]);
if(k) dp[i][j][k] = min(dp[i][j][k], id[ss[][k]-'a'][dp[i][j][k-]]);
}
}
}
}
}
else {
scanf("%d", &t);
if(t == ) --n1;
if(t == ) --n2;
if(t == ) --n3;
}
l1 = n1; l2 = n2; l3 = n3;
if(dp[l1][l2][l3] != n + ) puts("YES");
else puts("NO");
}
}
int main(){
Ac();
return ;
}
/*
6 8
abdabc
+ 1 a
+ 1 d
+ 2 b
+ 2 c
*/
CodeForces - 1150 D Three Religions的更多相关文章
- CF 1150 D Three Religions——序列自动机优化DP
题目:http://codeforces.com/contest/1150/problem/D 老是想着枚举当前在给定字符串的哪个位置,以此来转移. 所以想对三个串分别建 trie 树,然后求出三个t ...
- Codeforces 1149 B - Three Religions
B - Three Religions 思路:dp dp[i][j][k]:a的前i个和b的前j个和c的前k个能构成的最前面的位置 删字符时状态不用改变,加字符时只会改变1*250*250个状态 代码 ...
- Codeforces Round #556 (Div. 2) - D. Three Religions(动态规划)
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 3000 mSec Problem Descripti ...
- Codeforces Round #556 (Div. 2) D. Three Religions 题解 动态规划
题目链接:http://codeforces.com/contest/1150/problem/D 题目大意: 你有一个参考串 s 和三个装载字符串的容器 vec[0..2] ,然后还有 q 次操作, ...
- codeforces#1150D. Three Religions(dp+序列自动机)
题目链接: https://codeforces.com/contest/1150/problem/D 题意: 给出长度为$n$的字符串,和$q$次询问 每次询问是,给$x$宗教增加一个字符$key$ ...
- Three Religions CodeForces - 1149B (字符串,dp)
大意: 给定字符串S, 要求维护三个串, 支持在每个串末尾添加或删除字符, 询问S是否能找到三个不相交的子序列等于三个串. 暴力DP, 若不考虑动态维护的话, 可以直接$O(len^3)$处理出最少需 ...
- Codeforces 1050D Three Religions (dp+序列自动机)
题意: 给一个1e5的串str,然后有三个起始空串,不超过1000次操作,对三个字符串的一个尾部加一个字符或者减一个字符,保证每个字符不会超过250 每次操作之后询问你这三个串是不是可以组成str的子 ...
- Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集
C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...
- Educational Codeforces Round 5
616A - Comparing Two Long Integers 20171121 直接暴力莽就好了...没什么好说的 #include<stdlib.h> #include&l ...
随机推荐
- vue动态表单
项目需求,需要根据后台接口返回数据,动态添加表单内容 说明:此组件基于Ant Design of Vue 目前支持六种表单控件:文本输入框(TextInput).文本域输入框(TextArea).下拉 ...
- Find out "Who" and "Where"
Yesterday a friend of mine Kirby came to me with a smartphone and she wanted me to do her a favor. S ...
- Android UI绘制流程及原理
一.绘制流程源码路径 1.Activity加载ViewRootImpl ActivityThread.handleResumeActivity() --> WindowManagerImpl.a ...
- 优雅的对象转换解决方案-MapStruct使用进阶(二)
在前面, 介绍了 MapStruct 及其入门. 本文则是进一步的进阶. 在 MapStruct 生成对应的实现类的时候, 有如下的几个情景. 1 属性名称相同,则进行转化 在实现类的时候, 如果属性 ...
- alluxio源码解析-层次化存储(4)
层次化存储-特性介绍: https://www.alluxio.org/docs/1.6/cn/Tiered-Storage-on-Alluxio.html 引入分层存储后,Alluxio管理的数据块 ...
- hadoop hdfs 分布式存储
1.克隆前的工作 1.配置好网络nat 需要设置静态ip并能通过主机上网 ssh 和 rsync 是必须下载的 2.yum install vim wget rsync ssh 并配 ...
- SpringBoot操作ES进行各种高级查询
SpringBoot整合ES 创建SpringBoot项目,导入 ES 6.2.1 的 RestClient 依赖和 ES 依赖.在项目中直接引用 es-starter 的话会报容器初始化异常错误,导 ...
- 洛谷 P3648 [APIO2014]序列分割
题意简述 有一个长度为n的序列,分成k + 1非空的块, 选择两个相邻元素把这个块从中间分开,得到两个非空的块. 每次操作后你将获得那两个新产生的块的元素和的乘积的分数.求总得分最大值. 题解思路 f ...
- 100天搞定机器学习|day40-42 Tensorflow Keras识别猫狗
100天搞定机器学习|1-38天 100天搞定机器学习|day39 Tensorflow Keras手写数字识别 前文我们用keras的Sequential 模型实现mnist手写数字识别,准确率0. ...
- windows下用GCC编译DLL
此程序有3个文件,分别为 export.h .export.c .main.c export.h 文件内容 /*此头很有必要,别人在调用的时候知道有哪些方法*/ #ifdef BUILD_DLL #d ...