Problem  Codeforces Round #556 (Div. 2) - D. Three Religions

Time Limit: 3000 mSec

Problem Description

Input

Output

Sample Input

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

Sample Output

YES
YES
YES
YES
YES
YES
NO
YES

题解:动态规划,意识到这个题是动态规划之后难点在于要优化什么东西,本题是让判断原串能否划分成题中不断更新的三个字符串,通常情况下dp数组不仅仅记录true/false这种信息,因为这种信息往往可以在不改变复杂度的情况下通过记录更具体的信息来直接导出,而这些更具体的信息会给状态的转移带来便利,本题就是这样的情况。

  意识到本题的dp属于分段决策同样很重要,对于当前的三个字符串,判断是否合法的方式是逐个加入字符,逐个加入的过程就是天然的阶段,而每个阶段需要做出的决策是加入哪一个字符串的字符,在这个过程中维护的信息就是把第一个串的前 a 个字符,第二个串的前 b 个字符,第三个串的前 c 个字符放进去所需要原串的最小长度。

  有了这样的状态定义转移方程自然很简单,比如考虑dp[a][b][c],并且是从dp[a-1][b][c]转移过来的,那么dp[a][b][c]就是在dp[a-1][b][c]位置之后第一次出现第一个串第a个字符的位置,为了能够O(1)转移,预处理出对于原串的每个位置i,对每个小写英文字母x,i及i以后第一次出现x的位置,这很容易在O(26 * n)的复杂度内解决。这样每次状态转移只需要常数时间,正常情况下总的复杂度是O(q * 250^3),这肯定会T,但是考虑到每次新加入一个字符需要重新计算的dp值只有250^2个,因此复杂度实际为O(q * 250^2),可以接受。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0);
const int SIZE = + ;
const LL MOD = ; int n, q;
int type;
int Next[maxn][];
int dp[maxs][maxs][maxs];
string str, opt, word;
string ss[]; void cal(int a, int b, int c)
{
int &ans = dp[a][b][c];
ans = n;
if (a)
ans = min(ans, Next[dp[a - ][b][c] + ][ss[][a - ] - 'a']);
if (b)
ans = min(ans, Next[dp[a][b - ][c] + ][ss[][b - ] - 'a']);
if (c)
ans = min(ans, Next[dp[a][b][c - ] + ][ss[][c - ] - 'a']);
} void premanagement()
{
for (int i = ; i < ; i++)
{
Next[n][i] = Next[n + ][i] = n;
}
for (int i = n - ; i >= ; i--)
{
int tmp = str[i] - 'a';
for (int j = ; j < ; j++)
{
if (j != tmp)
Next[i][j] = Next[i + ][j];
else
Next[i][j] = i;
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout); cin >> n >> q;
cin >> str;
premanagement();
dp[][][] = -;
for (int i = ; i < q; i++)
{
cin >> opt >> type;
type--;
if (opt[] == '+')
{
cin >> word;
ss[type] += word[];
int max0 = ss[].size(), max1 = ss[].size(), max2 = ss[].size();
int min0 = (type == ? max0 : );
int min1 = (type == ? max1 : );
int min2 = (type == ? max2 : );
for (int a = min0; a <= max0; a++)
{
for (int b = min1; b <= max1; b++)
{
for (int c = min2; c <= max2; c++)
{
cal(a, b, c);
}
}
}
}
else
{
ss[type].pop_back();
} if (dp[ss[].size()][ss[].size()][ss[].size()] < n)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return ;
}

Codeforces Round #556 (Div. 2) - D. Three Religions(动态规划)的更多相关文章

  1. Codeforces Round #556 (Div. 2) D. Three Religions 题解 动态规划

    题目链接:http://codeforces.com/contest/1150/problem/D 题目大意: 你有一个参考串 s 和三个装载字符串的容器 vec[0..2] ,然后还有 q 次操作, ...

  2. Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...

  3. Codeforces Round #556 (Div. 1)

    Codeforces Round #556 (Div. 1) A. Prefix Sum Primes 给你一堆1,2,你可以任意排序,要求你输出的数列的前缀和中质数个数最大. 发现只有\(2\)是偶 ...

  4. Codeforces Round #556 (Div. 2)

    比赛链接 A 贪心 #include <cstdlib> #include <cstdio> #include <algorithm> #include <c ...

  5. Codeforces Round #556 (Div. 2)-ABC(这次的题前三题真心水)

    A. Stock Arbitraging 直接上代码: #include<cstdio> #include<cstring> #include<iostream> ...

  6. Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划

    C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...

  7. Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划

    C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...

  8. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  9. Codeforces Round #556 题解

    Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...

随机推荐

  1. Python学习笔记_二维数组的查找判断

    在进行数据处理的工作中,有时只是通过一维的list和有一个Key,一个value组成的字典,仍无法满足使用,比如,有三列.或四列,个数由不太多. 举一个现实应用场景:学号.姓名.手机号,可以再加元素 ...

  2. SqlServer——索引

    索引是根据表中一列或若干列按照一定顺序建立的列值与记录行之间的对应关系表.在数据库系统中建立索引主要有以下作用: l快速存取数据: l保证数据记录的唯一性: l实现表与表之间的参照完整性: l在使用O ...

  3. adf错误

    1>无法验证事务处理中的所有行 运行项目报错: javax.faces.el.EvaluationException: oracle.jbo.TxnValException: JBO-27023 ...

  4. 1.spring.net Look-up Method 查找方法的注入(方法是抽象的需要spring.net注入)

    .为什么需要查找方法的注入 当Object依赖另一个生命周期不同的Object,尤其是当singleton依赖一个non-singleton时,常会遇到不少问题,Lookup Method Injec ...

  5. 使用PPT演讲时,两个屏幕显示的内容不一样

    使用PPT演讲时,两个屏幕显示的内容不一样 摘自:http://blog.sina.com.cn/s/blog_75f3150b01018f3l.html 参考1:https://jingyan.ba ...

  6. Django学习笔记:为Model添加Action

    |- Django版本:1.8 |- Python版本:3.4 models.py 1 class Story(models.Model): #编辑Story状态 STATUS_CHOICES = ( ...

  7. Yii2.0 多语言设置(高级版配置方法) - 新的方法

    1.设置默认语言:在mail.php配置文件加上:'language'=>'zh_CN'; 2.多语言切换 (我这边是在site控制器里面操作的所以用的'/site/language') htm ...

  8. 关于innerHTML以及html2dom

    使用innerHTML或者insertAdjacentHTML 创建元素的时候能给我们带来很大的方便,为domNode 赋予innerHTML 属性,在插入大量的HTML的时候,使用innerHTML ...

  9. 虚拟化技术KVM

    1>虚拟化技术: 计算机虚拟化技术是多种技术的综合实现,它包括硬件平台,操作系统,存储以及网络等,简单地说,虚拟化技术就是在单台主机上可以虚拟多个虚假主机,并可以在这些虚拟主机上运行不同的操作系 ...

  10. python变量和简单的数据类型

    1.运行hello_world.py时发生的情况 运行hello_world.py时,Python都做了些什么呢?实际上,即便是运行简单的程序,Python所做的工作也相当多: #!/usr/bin/ ...