Codeforces Round #422 (Div. 2)E. Liar sa+st表+dp
题意:给你两个串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表就行了
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000009
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
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;}
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;}
using namespace std;
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=1000000+10,inf=0x3f3f3f3f;
char s[N],p[N];
int sa[N], t[N], t2[N], c[N], rk[N], height[N];
void buildSa(int n, int m) {
int i, j = 0, k = 0, *x = t, *y = t2;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = s[i]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
for(int k = 1; k < n; k <<= 1) {
int p = 0;
for(i = n - k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
for(int i = 1; i < n; i++) {
if(y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k])
x[sa[i]] = p - 1;
else x[sa[i]] = p++;
}
if(p >= n) break;
m = p;
}
for(i = 1; i < n; i++) rk[sa[i]] = i;
for(i = 0; i < n - 1; i++) {
if(k) k--;
j = sa[rk[i] - 1];
while(s[i + k] == s[j + k]) k++;
height[rk[i]] = k;
}
}
int Log[N];
struct ST {
int dp[N][20],ty;
void build(int n, int b[], int _ty) {
ty = _ty;
for(int i = 1; i <= n; i++) dp[i][0] = ty * b[i];
for(int j = 1; j <= Log[n]; j++)
for(int i = 1; i+(1<<j)-1 <= n; i++)
dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
}
int query(int x, int y) {
int k = Log[y - x + 1];
return ty * max(dp[x][k], dp[y-(1<<k)+1][k]);
}
}st;
int n,m,x;
int lcp(int x,int y)
{
x=rk[x],y=rk[y];
if(x>y)swap(x,y);x++;
return st.query(x,y);
}
int dp[100010][33];
int main()
{
for(int i = -(Log[0]=-1); i < N; i++)
Log[i] = Log[i - 1] + ((i & (i - 1)) == 0);
scanf("%d%s%d%s%d",&n,s,&m,p,&x);
s[n]='z'+1;
for(int i=n+1;i<n+1+m;i++)s[i]=p[i-n-1];
buildSa(n+m+2,258);
st.build(n+m+1,height,-1);
dp[0][0]=0;
for(int i=0;i<=n;i++)
{
for(int j=0;j<=x;j++)
{
if(dp[i][j]==m)return 0*puts("YES");
dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
if(j<x)
{
int lc=lcp(i,n+1+dp[i][j]);
dp[i+lc][j+1]=max(dp[i+lc][j+1],dp[i][j]+lc);
}
}
}
puts("NO");
return 0;
}
/********************
9
hloyaygrt
6
loyyrt
3
********************/
Codeforces Round #422 (Div. 2)E. Liar sa+st表+dp的更多相关文章
- 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 ...
- Codeforces Round #422 (Div. 2)
Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...
- 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 ...
- 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora
[题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...
- 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)
[题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...
- 【Codeforces Round #422 (Div. 2) B】Crossword solving
[题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...
- 【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 [题解] 哪个小就输出那个数的 ...
- 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 ...
- 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 ...
随机推荐
- python值json与pickle模块
#json 是用来序列化对象的 # 只有2个方法,序列化与反序列化 # 但是不能序列化类 与 函数 import json dict={"key1":[1,2,3,4,5]} f ...
- AL32UTF8 and UTF8 and ZHS16GBK
About AL32UTF8 ORACLE数据库字符集,即Oracle全球化支持(Globalization Support), 或即国家语言支持(NLS)其作用是用本国语言和格式来存储.处理和检索数 ...
- SQL中内连接和外连接的区别
数据表的连接有: 1.内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现 2.外连接: 包括 (1)左外连接(左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表 ...
- Linux防火墙iptables基础详解
原文来自于:https://www.linuxidc.com/Linux/2017-01/140073.htm(引用自) 一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分 ...
- go 语言
go语言(或 Golang)是Google在 2007 年开发的一种开源编程语言,于2009年11月开源,2012年发布go稳定版 go是非常年轻的一门语言,它的主要目标是“兼具Python 等动态语 ...
- delphi idhttp post 普通提交乱码处理
var IdHTTP1:TIdHTTP; postStream : TStringStream; Wstr:WideString; res:WideString; begin IdHTTP1 := T ...
- 一款非常好用的 Windows 服务开发框架,开源项目Topshelf
Topshelf是一个开发windows服务的比较好的框架之一,以下演示如何开发Topshelf服务. 1.首先打开你的vs.新建一个TopshelfStudy控制台程序,如下图所示: 这是我用vs2 ...
- Oracle 12c 单实例安装
准备工作 实验环境:Redhat 6.6 Oracle 12c 12.2.0.1 1.官网下载 https://www.oracle.com/technetwork/database/enterp ...
- MySQL中 Data truncated for column 'xxx'解决方法
DATA truncated FOR COLUMN 'description' AT ROW 1 1.错误再现 表中存在null字段 此时,修改表中某字段为主键 2.解决方法 不允许数据库中出现nul ...
- Linux 我的常用命令记录
Linux下复制粘贴快捷键 1. 在终端下: 复制命令:Ctrl + Shift + C 组合键. 粘贴命令:Ctrl + Shift + V 组合键. 2. 在控制台下: 复制命令:Ctrl + ...