E. Liar
 
 

The first semester ended. You know, after the end of the first semester the holidays begin. On holidays Noora decided to return to Vičkopolis. As a modest souvenir for Leha, she brought a sausage of length m from Pavlopolis. Everyone knows that any sausage can be represented as a string of lowercase English letters, the length of which is equal to the length of the sausage.

Leha was very pleased with the gift and immediately ate the sausage. But then he realized that it was a quite tactless act, because the sausage was a souvenir! So the hacker immediately went to the butcher shop. Unfortunately, there was only another sausage of length nin the shop. However Leha was not upset and bought this sausage. After coming home, he decided to cut the purchased sausage into several pieces and number the pieces starting from 1 from left to right. Then he wants to select several pieces and glue them together so that the obtained sausage is equal to the sausage that Noora gave. But the hacker can glue two pieces together only when the number of the left piece is less than the number of the right piece. Besides he knows that if he glues more than x pieces, Noora will notice that he has falsified souvenir sausage and will be very upset. Of course Leha doesn’t want to upset the girl. The hacker asks you to find out whether he is able to cut the sausage he bought, and then glue some of the pieces so that Noora doesn't notice anything.

Formally, you are given two strings s and t. The length of the string s is n, the length of the string t is m. It is required to select several pairwise non-intersecting substrings from s, so that their concatenation in the same order as these substrings appear in s, is equal to the string t. Denote by f(s, t) the minimal number of substrings to be chosen so that their concatenation is equal to the string t. If it is impossible to choose such substrings, then f(s, t) = ∞. Leha really wants to know whether it’s true that f(s, t) ≤ x.

Input

The first line contains single integer n (1 ≤ n ≤ 105) — length of sausage bought by Leha, i.e. the length of the string s.

The second line contains string s of the length n consisting of lowercase English letters.

The third line contains single integer m (1 ≤ m ≤ n) — length of sausage bought by Noora, i.e. the length of the string t.

The fourth line contains string t of the length m consisting of lowercase English letters.

The fifth line contains single integer x (1 ≤ x ≤ 30) — the maximum number of pieces of sausage that Leha can glue so that Noora doesn’t notice anything.

Output

In the only line print "YES" (without quotes), if Leha is able to succeed in creating new sausage so that Noora doesn't notice anything. Otherwise print "NO" (without quotes).

Examples
input
9
hloyaygrt
6
loyyrt
3
output
YES
 
Note

Let's consider the first sample.

In the optimal answer, Leha should cut the sausage he bought in the following way: hloyaygrt = h + loy + a + y + g + rt. Then he numbers received parts from 1 to 6:

  • h — number 1
  • loy — number 2
  • a — number 3
  • y — number 4
  • g — number 5
  • rt — number 6

Hereupon the hacker should glue the parts with numbers 2, 4 and 6 and get sausage loyygrt equal to one that is given by Noora. Thus, he will have to glue three pieces. Since x = 3 you should print "YES" (without quotes).

In the second sample both sausages coincide with sausages from the first sample. However since x = 2 you should print "NO" (without quotes).

题意:

  给你两个字符串,S,T

  你可以将S分成任意块编号,至多选出X块编号递增的顺序组成新的串

  问你是否能组成T

题解:

  很久没做后缀数组的题了,回顾了下

  设定dp[i][j],表示  0~(i-1)之前 挑选了j块 到达的最大位置

  那么对于S串的 i 位置,和T串的dp[i][j] + 1这个位置开始,最长公共前缀是多少?

  假设为t   , 那么转移就是 dp[i + t][j+1] = max(dp[i + t][j+1] , dp[i][j] + t);

  我们用后缀数组预处理出来lcp,那么查询的时候直接利用RMQ的O(1) 查询就好了

  整体复杂度O(nlogn + n*30)

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+, M = 1e3+,inf = 2e9+; int *ran,r[N],sa[N],height[N],wa[N],wb[N],wm[N];
bool cmp(int *r,int a,int b,int l) {
return r[a] == r[b] && r[a+l] == r[b+l];
}
void SA(int *r,int *sa,int n,int m) {
int *x=wa,*y=wb,*t;
for(int i=;i<m;++i)wm[i]=;
for(int i=;i<n;++i)wm[x[i]=r[i]]++;
for(int i=;i<m;++i)wm[i]+=wm[i-];
for(int i=n-;i>=;--i)sa[--wm[x[i]]]=i;
for(int i=,j=,p=;p<n;j=j*,m=p){
for(p=,i=n-j;i<n;++i)y[p++]=i;
for(i=;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=;i<m;++i)wm[i]=;
for(i=;i<n;++i)wm[x[y[i]]]++;
for(i=;i<m;++i)wm[i]+=wm[i-];
for(i=n-;i>=;--i)sa[--wm[x[y[i]]]]=y[i];
for(t=x,x=y,y=t,i=p=,x[sa[]]=;i<n;++i) {
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
}
ran=x;
}
void Height(int *r,int *sa,int n) {
for(int i=,j=,k=;i<n;height[ran[i++]]=k)
for(k?--k:,j=sa[ran[i]-];r[i+k] == r[j+k];++k);
}
int dp[N][];
int n,m,f[N][];
char s[N],t[N];
void Lcp_init() {
for(int i = ; i <= n+m+; ++i)
dp[i][] = height[i];
for(int j = ; (<<j) <= n + m + ; ++j) {
for(int i = ; i + (<<j) - <= n+m-; ++i) {
dp[i][j] = min(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
}
int lcp(int l,int r) {
l = ran[l], r = ran[r];
if(l > r) swap(l,r);
++l;
int len = r - l + ;
int k = ;
while((<<(k+)) <= len) ++k;
return min(dp[l][k], dp[r - (<<k) + ][k]);
}
int main() {
scanf("%d%s%d%s",&n,s,&m,t);
for(int i = ; i < n; ++i) r[i] = s[i] - 'a' + ;
r[n] = '*';
for(int i = n+; i < m+n+; ++i) r[i] = t[i - n - ] - 'a' + ;
r[n+m+] = ;
SA(r,sa,n+m++,);
Height(r,sa,n+m+);
Lcp_init();
int X;
scanf("%d",&X);
int mx = ;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= X; ++j) {
f[i+][j] = max(f[i+][j],f[i][j]);
int t = lcp(i,f[i][j] + n + );
f[i + t][j+] = max(f[i + t][j+] , f[i][j] + t);
mx = max(f[i][j],mx);
}
}
if(mx == m) puts("YES");
else puts("NO");
return ;
}
 

Codeforces Round #422 (Div. 2) E. Liar 后缀数组+RMQ+DP的更多相关文章

  1. 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 ...

  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 #244 (Div. 2)D (后缀自己主动机)

    Codeforces Round #244 (Div. 2)D (后缀自己主动机) (标号为0的节点一定是null节点,不管怎样都不能拿来用,切记切记,以后不能再错了) 这题用后缀自己主动机的话,对后 ...

  4. 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 ...

  5. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

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

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

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

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

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

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

  9. 【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 [题解] 哪个小就输出那个数的 ...

随机推荐

  1. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  2. Python GUI 之 Treeview 学习

    例子1 from tkinter import *import tkinter.ttk as ttk win = Tk()win.title("Treeview 学习") col ...

  3. BZOJ 2176 Strange string ——最小表示法

    本来想用来练习后缀自动机的,但是100w有点虚(事实证明确实T掉了). 只好上最小表示法. #include <cstdio> #include <cstring> #incl ...

  4. BZOJ 3747: [POI2015]Kinoman 【线段树】

    Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...

  5. 刷题总结——奇怪的游戏(scoi2012)

    题目: 题目描述 Blinker 最近喜欢上一个奇怪的游戏.这个游戏在一个 N*M  的棋盘上玩,每个格子有一个数.每次 Blinker  会选择两个相邻的格子,并使这两个数都加上 1.现在 Blin ...

  6. 洛谷 [P1939] 矩阵加速数列

    矩阵快速幂模版 #include <iostream> #include <cstring> #include <cstdlib> #include <alg ...

  7. leetcode 318. Maximum Product of Word Lengths

    传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...

  8. Hibrenate load 和 get

    这次我们来谈一下Hibernate3.2 Session加载数据时get和load方法的区别(Hibernate 3以后的版本就用get()方法取代find()这个方法了),其实这个在网上有很多的论述 ...

  9. Day 6 Linux基础之正文处理、vi编辑和系统初始化和服务

    Linux基础之正文处理.vi编辑和系统化服务 一.正文处理命令及tar命令 1.归档 定义:归档(archiving)就是将许多文件(或目录)打包成一个文件. 目的:归档的目的就是方便备份.还原及文 ...

  10. excel批量导入数据库SQL server

    思路: 第一是文件上传,可以参照Jakarta的FileUpload组件,用普通的Post也就行了.第二是Excel解析,用JSL或者POI都行第三是数据保存,这个应该简单吧,一个循环,一行对应一条数 ...