博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 1149 B - Three Religions
阅读量:6608 次
发布时间:2019-06-24

本文共 2771 字,大约阅读时间需要 9 分钟。

思路:dp

dp[i][j][k]:a的前i个和b的前j个和c的前k个能构成的最前面的位置

删字符时状态不用改变,加字符时只会改变1*250*250个状态

代码:

#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#include
using namespace std;#define y1 y11#define fi first#define se second#define pi acos(-1.0)#define LL long long#define ls rt<<1, l, m#define rs rt<<1|1, m+1, r//#define mp make_pair#define pb push_back#define ULL unsigned LL#define pll pair
#define pli pair
#define pii pair
#define piii pair
#define pdi pair
#define pdd pair
#define mem(a, b) memset(a, b, sizeof(a))#define debug(x) cerr << #x << " = " << x << "\n";#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//headconst int N = 1e5 + 5;char s[N];int nxt[N][26], n, q, x;char op[10], cc[10];int dp[255][255][255];string a, b, c;int main() { scanf("%d %d", &n, &q); scanf("%s", s+1); for (int i = 0; i < 26; ++i) nxt[n+1][i] = n+1, nxt[n+2][i] = n+1; for (int i = n; i >= 1; --i) { for (int j = 0; j < 26; ++j) { if(j == s[i]-'a') nxt[i][j] = i; else nxt[i][j] = nxt[i+1][j]; } } while(q--) { scanf("%s %d", op, &x); if(op[0] == '+') { scanf("%s", cc); if(x == 1){ a.push_back(cc[0]); for (int i = 0; i <= b.size(); ++i) { for (int j = 0; j <= c.size(); ++j) { dp[a.size()][i][j] = n+1; if(a.size() > 0) dp[a.size()][i][j] = min(dp[a.size()][i][j], nxt[dp[a.size()-1][i][j]+1][a.back()-'a']); if(i > 0) dp[a.size()][i][j] = min(dp[a.size()][i][j], nxt[dp[a.size()][i-1][j]+1][b[i-1]-'a']); if(j > 0) dp[a.size()][i][j] = min(dp[a.size()][i][j], nxt[dp[a.size()][i][j-1]+1][c[j-1]-'a']); } } } else if(x == 2) { b.push_back(cc[0]); for (int i = 0; i <= a.size(); ++i) { for (int j = 0; j <= c.size(); ++j) { dp[i][b.size()][j] = n+1; if(b.size() > 0) dp[i][b.size()][j] = min(dp[i][b.size()][j], nxt[dp[i][b.size()-1][j]+1][b.back()-'a']); if(i > 0) dp[i][b.size()][j] = min(dp[i][b.size()][j], nxt[dp[i-1][b.size()][j]+1][a[i-1]-'a']); if(j > 0) dp[i][b.size()][j] = min(dp[i][b.size()][j], nxt[dp[i][b.size()][j-1]+1][c[j-1]-'a']); } } } else if(x == 3) { c.push_back(cc[0]); for (int i = 0; i <= a.size(); ++i) { for (int j = 0; j <= b.size(); ++j) { dp[i][j][c.size()] = n+1; if(c.size() > 0) dp[i][j][c.size()] = min(dp[i][j][c.size()], nxt[dp[i][j][c.size()-1]+1][c.back()-'a']); if(i > 0) dp[i][j][c.size()] = min(dp[i][j][c.size()], nxt[dp[i-1][j][c.size()]+1][a[i-1]-'a']); if(j > 0) dp[i][j][c.size()] = min(dp[i][j][c.size()], nxt[dp[i][j-1][c.size()]+1][b[j-1]-'a']); } } } } else { if(x == 1) a.pop_back(); else if(x == 2) b.pop_back(); else if(x == 3) c.pop_back(); } if(dp[a.size()][b.size()][c.size()] > n) printf("NO\n"); else printf("YES\n"); } return 0;}

 

转载于:https://www.cnblogs.com/widsom/p/10808527.html

你可能感兴趣的文章
读人月神话有感
查看>>
Python第一天
查看>>
管理工具
查看>>
未找到工作先遇黑客 八大人事局网站被挂马
查看>>
VB100十二月成绩公布
查看>>
ifanr访谈:GuruDigger — Web工程师排排坐
查看>>
Windows Phone 7新开发工具发布
查看>>
一起谈.NET技术,利用Response.Flush和iframe实现”服务器推”技术
查看>>
一起谈.NET技术,七种武器武装.NET(常用开发工具介绍)
查看>>
一起谈.NET技术,解决编程中序列化问题
查看>>
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
查看>>
旅行的牧歌
查看>>
volatile与restrict
查看>>
nodejs基础 -- express框架
查看>>
500. Keyboard Row
查看>>
260. Single Number III
查看>>
第8周课下作业2(补)
查看>>
jmeter自动化接口
查看>>
Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】...
查看>>
iOS开发笔记--Masonry介绍与使用实践:快速上手Autolayout
查看>>