Codeforces Round #422 (Div. 2) E. Liar suffix Array +rmq+dp

Source: Internet
Author: User

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 中文版 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 is 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 isn't 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 F Rom left-to-right. Then he wants to select several pieces and glue them together so that the obtained sausage are equal to the sausage that No Ora gave. But the hacker can glue and pieces together only when the number of the left piece was less than the number of the right pi Ece. Besides he knows that if he glues more than x pieces, Noora would notice that he had falsified souvenir sausag E and would be very upset. Of course Leha doesn ' t want to upset the girl. The hacker asks you-find out whether he's able to cut the sausage he bought, and then glue some of the pieces so Noora doesn ' t notice anything.

Formally, given, stringssandT. The length of the stringsIs n , the length of the String  t  is < Span class= "Tex-span" > m . It is required to select several pairwise non-intersecting substrings from  s , so tha T their concatenation in the same order as these substrings appear In  s , was equal to The String  t . Denote By  F ( s , t )  the minimal number of substrings to be Chosen so, their concatenation is equal to the String  t . If It is impossible to choose such substrings, Then  F ( s , t ) =∞. Le Ha really wants to know whether it ' s true that  F ( s , t ) ≤ x.

Input

The first line contains a 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 中文版 letters.< /c11>

The third line contains single integer m (1≤ mn)-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 中文版 letters.

The fifth line contains a single integer x (1≤ x ≤30)-the maximum number of pieces of sausage That's Leha can glue so, Noora doesn ' t notice anything.

Output

In the ' line print 'YES ' (without quotes), if Leha is able to succeed ' 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 a-type: HLOYAYGRT = h + loy + A + y + G + r T. Then he numbers received parts from 1 to 6:

    • h -number 1
    • Span class= "Tex-font-style-tt" >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 tha T is given by Noora. Thus, he'll has to glue three pieces. Since x = 3 You should print "YES" (without quotes).

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

Test instructions

Here's a two string for you,s,t .

You can divide S into any block number, and at most select the order in which the X -block number increments to form a new string.

Ask if you can make a T -string

Exercises

Long time did not do the suffix array of questions, reviewed the next

Set Dp[i][j], indicating 0~ (i-1) before picking the maximum position of J block reached

So what is the longest common prefix for the i position of the S -String, and the dp[i][j of T -string ] + 1 ?

Assuming t , then the transfer is dp[i + t][j+1] = max (Dp[i + t][j+1], Dp[i][j] + t);

We use the suffix array to preprocess the LCP, then query the time directly using rmq O (1) query just fine

Overall complexity O (Nlogn + n*30)

#include <bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineLS i<<1#defineRS ls | 1#defineMid ((LL+RR) >>1)#definePII pair<int,int>#defineMP Make_pairtypedefLong LongLL;Const Long LongINF = 1e18+1LL;Const DoublePI = ACOs (-1.0);Const intN = 5e5+5, M = 1e3+ -, INF = 2e9+Ten;int*Ran,r[n],sa[n],height[n],wa[n],wb[n],wm[n];BOOLcmpint*r,intAintBintl) {returnR[a] = = R[b] && r[a+l] = = r[b+l];}voidSA (int*r,int*sa,intNintm) {int*x=wa,*y=wb,*T;  for(intI=0; i<m;++i) wm[i]=0;  for(intI=0; i<n;++i) wm[x[i]=r[i]]++;  for(intI=1; i<m;++i) wm[i]+=wm[i-1];  for(inti=n-1; i>=0;-I.) sa[--wm[x[i]]]=i;  for(intI=0, j=1, p=0;p <n;j=j*2, m=p) {         for(p=0, I=n-j;i<n;++i) y[p++]=i;  for(i=0; i<n;++i)if(SA[I]&GT;=J) y[p++]=sa[i]-J;  for(i=0; i<m;++i) wm[i]=0;  for(i=0; i<n;++i) wm[x[y[i]]]++;  for(i=1; i<m;++i) wm[i]+=wm[i-1];  for(i=n-1; i>=0;-I.) sa[--wm[x[y[i]]]]=Y[i];  for(t=x,x=y,y=t,i=p=1, x[sa[0]]=0; i<n;++i) {X[sa[i]]=CMP (y,sa[i],sa[i-1],j)? p1:p + +; }} ran=x;}voidHeight (int*r,int*sa,intN) { for(intI=0, j=0, k=0; i<n;height[ran[i++]]=k) for(k?--K:0, j=sa[ran[i]-1];r[i+k] = = r[j+k];++k);}intdp[n][ -];intn,m,f[n][ -];CharS[n],t[n];voidLcp_init () { for(inti =1; I <= n+m+1; ++i) dp[i][0] =Height[i];  for(intj =1; (1&LT;&LT;J) <= n + M +1; ++j) { for(inti =1; i + (1&LT;&LT;J)-1<= n+m-1; ++i) {dp[i][j]= Min (dp[i][j-1],dp[i+ (1<< (J-1))][j-1]); }    }}intLcpintLintr) {L= Ran[l], r =Ran[r]; if(L >r) Swap (L,R); ++l; intLen = r-l +1; intK =0;  while((1<< (k +1)) <= Len) + +K; returnMin (Dp[l][k], Dp[r-(1&LT;&LT;K) +1][k]);}intMain () {scanf ("%d%s%d%s",&n,s,&m,t);  for(inti =0; I < n; ++i) R[i] = s[i]-'a'+1; R[n]='*';  for(inti = n+1; I < m+n+1; ++i) R[i] = t[i-n-1] -'a'+1; R[n+m+1] =0; SA (R,sa,n+m+1+1, the); Height (R,sa,n+m+1);    Lcp_init (); intX; scanf ("%d",&X); intMX =0;  for(inti =0; I <= N; ++i) { for(intj =0; J <= X; ++j) {F[i+1][J] = max (f[i+1][j],f[i][j]); intt = LCP (I,f[i][j] + n +1); F[i+ t][j+1] = max (f[i + t][j+1], F[i][j] +t); MX=Max (F[I][J],MX); }    }    if(mx = = m) puts ("YES"); ElsePuts"NO"); return 0;}

Codeforces Round #422 (Div. 2) E. Liar suffix Array +rmq+dp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.