BZOJ_1622_[Usaco2008_Open]_Word_Power_名字的能量_(字符匹配_暴力)
描述
http://www.lydsy.com/JudgeOnline/problem.php?id=1622
给出多个文本串和模式串,求每个文本串中有多少模式串.
分析
直接暴力...
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=1000+5,maxm=100+5,maxl=30+5; 5 int n,m; 6 int t[maxn][maxn],p[maxm][maxl]; 7 char c; 8 char s[maxn]; 9 inline bool match(int x,int y){ 10 int l1=t[x][0],l2=p[y][0],i,j; 11 for(i=1,j=1;i<=l1&&j<=l2;i++)if(t[x][i]==p[y][j]) j++; 12 return j>l2; 13 } 14 int main(){ 15 scanf("%d%d",&n,&m); 16 for(int i=1;i<=n;i++){ 17 scanf("%s",s+1); int l=strlen(s+1); t[i][0]=l; 18 for(int j=1;j<=l;j++) t[i][j]=s[j]>='a'&&s[j]<='z'?s[j]-'a':s[j]-'A'; 19 } 20 for(int i=1;i<=m;i++){ 21 scanf("%s",s+1); int l=strlen(s+1); p[i][0]=l; 22 for(int j=1;j<=l;j++) p[i][j]=s[j]>='a'&&s[j]<='z'?s[j]-'a':s[j]-'A'; 23 } 24 for(int i=1;i<=n;i++){ 25 int ans=0; 26 for(int j=1;j<=m;j++)if(match(i,j)) ans++; 27 printf("%d\n",ans); 28 } 29 return 0; 30 }
1622: [Usaco2008 Open]Word Power 名字的能量
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 496 Solved: 253
[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".
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".
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