1622: [Usaco2008 Open]Word Power 名字的能量
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 370 Solved: 184
[Submit][Status][Discuss]
Description
约翰想要计算他那N(1≤N≤1000)只奶牛的名字的能量.每只奶牛的名字由不超过1000个字待构成,没有一个名字是空字体串, 约翰有一张“能量字符串表”,上面有M(1≤M≤100)个代表能量的字符串.每个字符串由不超过30个字体构成,同样不存在空字符串.一个奶牛的名字蕴含多少个能量字符串,这个名字就有多少能量.所谓“蕴含”,是指某个能量字符串的所有字符都在名字串中按顺序出现(不一定一个紧接着一个).
所有的大写字母和小写字母都是等价的.比如,在贝茜的名字“Bessie”里,蕴含有“Be”
“sI”“EE”以及“Es”等等字符串,但不蕴含“lS”或“eB”.请帮约翰计算他的奶牛的名字的能量.
Input
第1行输入两个整数N和M,之后N行每行输入一个奶牛的名字,之后M行每行输入一个能量字符串.
Output
一共N行,每行一个整数,依次表示一个名字的能量.
Sample Input
5 3
Bessie
Jonathan
Montgomery
Alicia
Angola
se
nGo
Ont
INPUT DETAILS:
There are 5 cows, and their names are "Bessie", "Jonathan",
"Montgomery", "Alicia", and "Angola". The 3 good strings are "se",
"nGo", and "Ont".
Sample Output
1
1
2
0
1
OUTPUT DETAILS:
"Bessie" contains "se", "Jonathan" contains "Ont", "Montgomery" contains
both "nGo" and "Ont", Alicia contains none of the good strings, and
"Angola" contains "nGo".
HINT
Source
题解:这道题显然可以暴力随便谢谢水过,但要是这样子的话就远远没有辣么好玩了,于是我再一次请出了萌萌哒线段树——
线段树存储每一个名字,然后连维护都不需要,直接实现查找在某某区间范围内最靠左的某个指定字母的位置,然后有了这个,就可以直接用后面的子串对前面的名字进行匹配即可,复杂度O(NMLlogM)(居然还是2988 ms水过去了,好开心)
1 /************************************************************** 2 Problem: 1622 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:2988 ms 7 Memory:2448 kb 8 ****************************************************************/ 9 10 var 11 i,j,k,l,m,n,ans:longint; 12 ss:ansistring; 13 b,c:array[0..10000] of ansistring; 14 a:array[0..10000,0..26] of longint; 15 function min(x,y:longint):longint;inline; 16 begin 17 if x<y then min:=x else min:=y; 18 end; 19 function max(x,y:longint):longint;inline; 20 begin 21 if x>y then max:=x else max:=y; 22 end; 23 procedure built(z,x,y:longint); 24 var i:longint; 25 begin 26 if (x=y) then 27 a[z,ord(ss[x])-64]:=1 28 else 29 begin 30 built(z*2,x,(x+y) div 2); 31 built(z*2+1,(x+y) div 2+1,y); 32 for i:=1 to 26 do a[z,i]:=a[z*2,i]+a[z*2+1,i] 33 end; 34 end; 35 function approach(z,x,y,l,r,t:longint):longint; 36 var a1:longint; 37 begin 38 if l>r then exit(0); 39 if a[z,t]=0 then exit(0); 40 if x=y then exit(x); 41 a1:=approach(z*2,x,(x+y) div 2,l,min(r,(x+y) div 2),t); 42 if a1<>0 then exit(a1); 43 exit(approach(z*2+1,(x+y) div 2+1,y,max(l,(x+y) div 2+1),r,t)); 44 end; 45 46 47 begin 48 readln(n,m); 49 for i:=1 to n do 50 begin 51 readln(b[i]); 52 b[i]:=upcase(b[i]); 53 end; 54 for i:=1 to m do 55 begin 56 readln(c[I]); 57 c[i]:=upcase(c[i]); 58 end; 59 for i:=1 to n do 60 begin 61 ss:=b[i]; 62 fillchar(a,sizeof(a),0); 63 built(1,1,length(ss)); 64 ans:=0; 65 for j:=1 to m do 66 begin 67 l:=0; 68 for k:=1 to length(c[j]) do 69 begin 70 l:=approach(1,1,length(ss),l+1,length(ss),ord(c[j][k])-64); 71 if l=0 then break; 72 end; 73 if l<>0 then inc(ans); 74 end; 75 writeln(ans); 76 end; 77 end.