tyvj1068 STR
生活随笔
收集整理的這篇文章主要介紹了
tyvj1068 STR
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給你兩個串A,B,可以得到從A的任意位開始的子串和B匹配的長度。
給定K個詢問,對于每個詢問給定一個x,求出匹配長度恰為x的位置有多少個。
N,M,K<=200000
字符串的處理,用KMP嘍,首先求出模式串B的next[],然后進行一遍AB匹配,記錄a[i]匹配的最大長度,那么如果a[i]可以匹配l[i]的長度,answer[i]表示匹配長度為i的串的數量,則answer[next[i]]的匹配長度也是滿足answer[i]的,所以只要倒著循環一遍,把answer[next[i]]加到answer[i]中即可。這時的answer[i]包含了長度為i+1的情況,所以要輸出answer[k]-answer[k+1].
View Code 1 program tyvj1068(input,output);2 var
3 a,b : ansistring;
4 next,l : array[0..201000] of longint;
5 answer : array[0..201000] of longint;
6 i,j,x : longint;
7 n,m,k : longint;
8 ch : char;
9 begin
10 readln(n,m,k);
11 a:='';
12 b:='';
13 for i:=1 to n do
14 begin
15 read(ch);
16 a:=a+ch;
17 end;
18 readln;
19 for i:=1 to m do
20 begin
21 read(ch);
22 b:=b+ch;
23 end;
24 readln;
25 fillchar(next,sizeof(next),0);
26 next[1]:=0;
27 j:=0;
28 for i:=2 to m do
29 begin
30 while (j>0)and(b[j+1]<>b[i]) do
31 j:=next[j];
32 if b[j+1]=b[i] then
33 inc(j);
34 next[i]:=j;
35 end;
36 j:=0;
37 for i:=1 to n do
38 begin
39 while (j>0)and(b[j+1]<>a[i]) do
40 j:=next[j];
41 if b[j+1]=a[i] then
42 inc(j);
43 l[i]:=j;
44 if j=m then
45 j:=next[j];
46 end;
47 fillchar(answer,sizeof(answer),0);
48 for i:=1 to n do
49 inc(answer[l[i]]);
50 for i:=n downto 1 do
51 inc(answer[next[i]],answer[i]);
52 for i:=1 to k do
53 begin
54 readln(x);
55 writeln(answer[x]-answer[x+1]);
56 end;
57 end.
轉載于:https://www.cnblogs.com/neverforget/archive/2012/03/27/2419621.html
總結
以上是生活随笔為你收集整理的tyvj1068 STR的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: boost学习之 时间和日期 time
- 下一篇: XEN 第一天