Facer ' s string
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 1879 |
|
Accepted: 568 |
Description
Minifacer was very happy these days because he had learned the algorithm of KMP recently. Yet his elder brother, Hugefacer, thought, Minifacer needs a deeper understanding of this algorithm. Thus Hugefacer decided to play a game with he little brother to enhance his skills.
First, Hugefacer wrote down, stringsS1andS2. Then Minifacer tried to find a substringS3OfS1Which meets the following requirements:1)S3Should has a length ofk(which is a constant value); 2)S3should also be the substring ofS2. After several rounds, Hugefacer found, the This game is too easy for him clever little brother, so he added another requi Rement:3) The extended string ofS3Should not being the substring ofS2. Here the extended string ofS3is defined asS3Plus its succeed character inS1(IfS3Does not has a succeed character inS1The extended string ofS3IsS3+ ' which'll never appear inS2). For example, letS1Be ' ababc ', if we select the substring from the first character to the second character asS3(SoS3equals "AB"), its extended string should is "ABA"; If we select the substring from the third character to the fourth character asS3, its extended string should is "ABC"; If we select the substring from the fourth character to the fifth character asS3, its extended string should is "BC".
Since the difficult level of the game have been greatly increased after the third requirement was added, Minifacer is not Able to win the game and he thought this maybe none of the substring would meet all the requirements. In order to prove that minifacer is wrong, Hugefacer would like to write a program to compute number of substrings that M The three demands (Note that both strings with same appearance but different positions in original string S1 should is Count twice). Since Hugefacer does not like characters, he'll use non-negative integers (range from 0 to 10000) instead.
Input
There is multiple test cases. Contains three lines:the first line contains three integersn, m and K where n represents the length of S1, m represents the length of S2 andK represents the Leng th of substring; The second line contains string S1 and the third line contains string S2. Here 0≤n, m ≤50000. Input ends with EOF.
Output
For each test case, output a number in a line stand for the total number of substrings that meet the three requirements.
Sample Input
5 5 21 2 1 2 31 2 3 4 55 5 31 2 1 2 31 2 3 4 5
Sample Output
21st
Source
POJ Monthly contest–2009.04.05, Facer
The substring of a string has several lcp==k that can and all the substrings of the B string.
See the problem in addition to use SA+RMQ violence LCP no idea, and this is definitely a time-out, so with the help of powerful Baidu, reference to someone else's code, very ingenious, linear scan, because the request is just K, you can ask for greater than or equal to K minus greater than or equal to k+1, the two strings together, Use the height array to group the suffixes, and see how several of the first strings in each group are the second string, and if it's the first string, no, no, plus the number of the first string.
AC Code
problem:3729 user:kxh1995 memory:2512k time:657ms language:c++
#include <stdio.h> #include <string.h> #include <algorithm> #incl ude<iostream> #define MIN (A, b) (A>B?B:A) #define MAX (A, B) (a>b?a:b) using NA Mespace std; #define INF 0x3f3f3f3f Char str[103030]; int sa[103030],rank[103030],rank2[103030],height[103030],c[103030],*x,*y,s[100030]; void cmp (int n,int sz) {int i; Memset (C,0,sizeof (c)); for (i=0;i<n;i++) c[x[y[i]]]++; for (i=1;i<sz;i++) c[i]+=c[i-1]; for (i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; } void Build_sa (int *s,int n,int sz) {x=rank,y=rank2; int i,j; for (i=0;i<n;i++) x[i]=s[i],y[i]=i; CMP (N,SZ); int Len; for (len=1;len<n;len<<=1) {int yid=0; for (i=n-len;i<n;i++) {y[yid++]=i; } for (i=0;i<n;i++) if (Sa[i]>=len) Y[yid++]=sa[i]-len; CMP (N,SZ); Swap (x, y); X[sa[0]]=yid=0; for (i=1;i<n;i++) {if (y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+l En<n&&y[sa[i-1]+len]==y[sa[i]+len]) X[sa[i]]=yid; else X[sa[i]]=++yid; } sz=yid+1; if (sz>=n) break; } for (i=0;i<n;i++) rank[i]=x[i]; } void getheight (int *s,int n) {int k=0; for (int i=0;i<n;i++) {if (rank[i]==0) continue; K=max (0,k-1); int j=sa[rank[i]-1]; while (S[i+k]==s[j+k]) k++; Height[rank[i]]=k; }} int N,m,k,len;__int64 fun (int k) {int l,r,i;l=0;r=0;__int64 ans=0;for (i=1;i<=len;i++) {if (height[i]<k) {i F (r) ans+= (__int64) l;l=r=0;if (sa[i]<n) l++;elser++;} Else{if (Sa[i]<n) l++;elser++;}} return ans;} int main () {while (scanf ("%d%d%d", &n,&m,&k)!=eof) {int i;for (i=0;i<n;i++) {scanf ("%d", &s[i]); S[i] ++;} S[n]=10002;len=n+m+1;for (i=n+1;i<len;i++) {scanf ("%d", &s[i]); s[i]++;} S[len]=0;build_sa (s,len+1,10003); GetHeight (S,len);p rintf ("%i64d\n", Fun (k)-fun (k+1));}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ topic 3229 Facer ' s string (suffix array to find a string length of K substring with several occurrences in string b)