题意:给你两个串s,p,问你把s分开顺序不变,能不能用最多k段合成p.

题解:dp[i][j]表示s到了前i项,用了j段的最多能合成p的前缀是哪里,那么转移就是两种,\(dp[i+1][j]=dp[i][j],dp[i+lcp][j+1]=dp[i][j]+lcp\),这里的lcp是dp[i][j]和i的lcp,然后sa预处理一下st表就行了

  1. //#pragma GCC optimize(2)
  2. //#pragma GCC optimize(3)
  3. //#pragma GCC optimize(4)
  4. //#pragma GCC optimize("unroll-loops")
  5. //#pragma comment(linker, "/stack:200000000")
  6. //#pragma GCC optimize("Ofast,no-stack-protector")
  7. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  8. #include<bits/stdc++.h>
  9. #define fi first
  10. #define se second
  11. #define db double
  12. #define mp make_pair
  13. #define pb push_back
  14. #define pi acos(-1.0)
  15. #define ll long long
  16. #define vi vector<int>
  17. #define mod 1000000009
  18. #define ld long double
  19. //#define C 0.5772156649
  20. //#define ls l,m,rt<<1
  21. //#define rs m+1,r,rt<<1|1
  22. #define pll pair<ll,ll>
  23. #define pil pair<int,ll>
  24. #define pli pair<ll,int>
  25. #define pii pair<int,int>
  26. #define ull unsigned long long
  27. //#define base 1000000000000000000
  28. #define fin freopen("a.txt","r",stdin)
  29. #define fout freopen("a.txt","w",stdout)
  30. #define fio ios::sync_with_stdio(false);cin.tie(0)
  31. inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  32. inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
  33. inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
  34. template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
  35. template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
  36. inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
  37. inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
  38. using namespace std;
  39. const ull ba=233;
  40. const db eps=1e-5;
  41. const ll INF=0x3f3f3f3f3f3f3f3f;
  42. const int N=200000+10,maxn=1000000+10,inf=0x3f3f3f3f;
  43. char s[N],p[N];
  44. int sa[N], t[N], t2[N], c[N], rk[N], height[N];
  45. void buildSa(int n, int m) {
  46. int i, j = 0, k = 0, *x = t, *y = t2;
  47. for(i = 0; i < m; i++) c[i] = 0;
  48. for(i = 0; i < n; i++) c[x[i] = s[i]]++;
  49. for(i = 1; i < m; i++) c[i] += c[i - 1];
  50. for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
  51. for(int k = 1; k < n; k <<= 1) {
  52. int p = 0;
  53. for(i = n - k; i < n; i++) y[p++] = i;
  54. for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
  55. for(i = 0; i < m; i++) c[i] = 0;
  56. for(i = 0; i < n; i++) c[x[y[i]]]++;
  57. for(i = 1; i < m; i++) c[i] += c[i - 1];
  58. for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
  59. swap(x, y);
  60. p = 1; x[sa[0]] = 0;
  61. for(int i = 1; i < n; i++) {
  62. if(y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k])
  63. x[sa[i]] = p - 1;
  64. else x[sa[i]] = p++;
  65. }
  66. if(p >= n) break;
  67. m = p;
  68. }
  69. for(i = 1; i < n; i++) rk[sa[i]] = i;
  70. for(i = 0; i < n - 1; i++) {
  71. if(k) k--;
  72. j = sa[rk[i] - 1];
  73. while(s[i + k] == s[j + k]) k++;
  74. height[rk[i]] = k;
  75. }
  76. }
  77. int Log[N];
  78. struct ST {
  79. int dp[N][20],ty;
  80. void build(int n, int b[], int _ty) {
  81. ty = _ty;
  82. for(int i = 1; i <= n; i++) dp[i][0] = ty * b[i];
  83. for(int j = 1; j <= Log[n]; j++)
  84. for(int i = 1; i+(1<<j)-1 <= n; i++)
  85. dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
  86. }
  87. int query(int x, int y) {
  88. int k = Log[y - x + 1];
  89. return ty * max(dp[x][k], dp[y-(1<<k)+1][k]);
  90. }
  91. }st;
  92. int n,m,x;
  93. int lcp(int x,int y)
  94. {
  95. x=rk[x],y=rk[y];
  96. if(x>y)swap(x,y);x++;
  97. return st.query(x,y);
  98. }
  99. int dp[100010][33];
  100. int main()
  101. {
  102. for(int i = -(Log[0]=-1); i < N; i++)
  103. Log[i] = Log[i - 1] + ((i & (i - 1)) == 0);
  104. scanf("%d%s%d%s%d",&n,s,&m,p,&x);
  105. s[n]='z'+1;
  106. for(int i=n+1;i<n+1+m;i++)s[i]=p[i-n-1];
  107. buildSa(n+m+2,258);
  108. st.build(n+m+1,height,-1);
  109. dp[0][0]=0;
  110. for(int i=0;i<=n;i++)
  111. {
  112. for(int j=0;j<=x;j++)
  113. {
  114. if(dp[i][j]==m)return 0*puts("YES");
  115. dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
  116. if(j<x)
  117. {
  118. int lc=lcp(i,n+1+dp[i][j]);
  119. dp[i+lc][j+1]=max(dp[i+lc][j+1],dp[i][j]+lc);
  120. }
  121. }
  122. }
  123. puts("NO");
  124. return 0;
  125. }
  126. /********************
  127. 9
  128. hloyaygrt
  129. 6
  130. loyyrt
  131. 3
  132. ********************/

Codeforces Round #422 (Div. 2)E. Liar sa+st表+dp的更多相关文章

  1. Codeforces Round #422 (Div. 2) E. Liar 后缀数组+RMQ+DP

    E. Liar     The first semester ended. You know, after the end of the first semester the holidays beg ...

  2. Codeforces Round #422 (Div. 2)

    Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...

  3. Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

    Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...

  4. 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora

    [题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...

  5. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)

    [题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...

  6. 【Codeforces Round #422 (Div. 2) B】Crossword solving

    [题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...

  7. 【Codeforces Round #422 (Div. 2) A】I'm bored with life

    [题目链接]:http://codeforces.com/contest/822/problem/A [题意] 让你求a!和b!的gcd min(a,b)<=12 [题解] 哪个小就输出那个数的 ...

  8. Codeforces Round #422 (Div. 2) D. My pretty girl Noora 数学

    D. My pretty girl Noora     In Pavlopolis University where Noora studies it was decided to hold beau ...

  9. Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心

    C. Hacker, pack your bags!     It's well known that the best way to distract from something is to do ...

随机推荐

  1. PyCharm:no module named * 解决方法

    1.成功安装模块,无法导入 今天安装完模块pyppeteer,pycharm导入失败,从python的Lib下可以清楚的看到已经安装成功 2.添加当前python环境,不使用默认项目的环境 file& ...

  2. 平衡树-Splay

    #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #def ...

  3. 第一个Spring程序HelloWorld

    对于初学者而言,任何理论化的讲解都比不上一个简单的HelloWorld,我们在学习Spring时也不外乎用最简单的HelloWorld程序来将这个灵活而又强大的轻量级框架推送到诸位面前.想要说明的是现 ...

  4. 易爆物D305

    分析:典型的并查集,每一个物品合一看成一个独立的顶点,则一个简单化合物就是一条边,如果两个顶点x,y联通则说明有危险,所以可以用一个并查集来维护图的联通分量集合,并查集的详解有一篇写的很易懂的博客并查 ...

  5. python中sorted()和set()去重,排序

    前言 在看一个聊天机器人的神经网络模型训练前准备训练数据,需要对训练材料做处理(转化成张量)需要先提炼词干,然后对词干做去重和排序 words = sorted(list(set(words))) 对 ...

  6. 2018-2019-2 20165215《网络对抗技术》Exp5 MSF基础应用

    目录 实验内容 实验原理 实验步骤 (一)一个主动攻击实践 ms08_067(成功) (二)一个针对浏览器的攻击 ms14_064(成功) (三)一个针对客户端的攻击 Adobe(成功) CVE-20 ...

  7. 小米手机跨域问题,返回resphone:undefined,status 0

    小米手机跨域问题,返回resphone:undefined,status 0我小米note2的手机登录不上,返回resphone:undefined,status 0 我手机登录不了的问题解决了,后台 ...

  8. Android SDK Manager for Mac 在线更新镜像地址截至2017-10-01亲测有效

    虽然国内google被墙了,但仍可利用国内的某些镜像网站实现Android SDK在线更新,使用方法如下: 1.启动 Android SDK Manager ,打开主界面,依次选择『Tools』.『O ...

  9. 2.获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到F盘下任意目录

    package cn.it.text; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...

  10. java eclipse war包的二次开发方法

    有实际项目在跑的war包,却没有源码,苦于想查看源码,身处运维组为研发组看不起,拿不到源码,只能自己来反编译了. 只要你细心点,其实在解压war包后,可以看到文件夹中,已经存在了jsp文件,但是却没有 ...