[Usaco2008 Open]Word Power 名字的能量

1622: [Usaco2008 Open]Word Power 名字的能量

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 408  Solved: 198
[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

Silver 

                        [Submit][Status][Discuss]

 

  本来想找一道字符串的题,看着像是一道模拟,直接爆搜,但是超时了好几次。。。下面是两种写法,一个是6100多毫秒个计时器掐了,另一种却是200多毫秒AC。。。。

  先来个超时的。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<bits/stdc++.h>
 6 using namespace std;
 7 int N,M;
 8 char T[2000][2000];
 9 char P[200][50];
10 int lenT[2000];
11 int lenP[200];
12 int main(){
13     
14     cin>>N>>M;
15     for(int i=1;i<=N;i++) scanf("%s",T[i]);
16     for(int i=1;i<=M;i++) scanf("%s",P[i]); 
17 
18      
19     for(int i=1;i<=N;i++){
20         for(int j=0;j<=strlen(T[i])-1;j++){
21             if(int(T[i][j])>=97)
22                 T[i][j]=char(int(T[i][j])-32);
23         }
24     }
25     
26     for(int i=1;i<=M;i++){
27         for(int j=0;j<=strlen(P[i])-1;j++){
28             if(int(P[i][j])>=97)
29                 P[i][j]=char(int(P[i][j])-32);
30         }
31     }
32     
33     
34     for(int i=1;i<=N;i++){
35         int ANS=0;
36         for(int j=1;j<=M;j++){
37             int now=0;
38             int len=strlen(P[j]); 
39             for(int k=0;k<=strlen(T[i])-1;k++){
40                 if(T[i][k]==P[j][now]){
41                     now++;
42                     if(now==len){
43                         ANS++;
44                         break;
45                     } 
46                 }
47             }
48         
49         }
50         cout<<ANS<<endl;
51     }
52     
53     return 0;
54 }

 

 然后改成这个就AC了,至少快了30多倍。。。差别很大么?

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<bits/stdc++.h>
 6 using namespace std;
 7 int N,M;
 8 char T[2000][2000];
 9 char P[200][50];
10 int lenT[2000];
11 int lenP[200];
12 int main(){
13     
14     cin>>N>>M;
15 
16     for (int i=1;i<=N;i++){
17         scanf("%s",T[i]);
18         while (T[i][lenT[i]])
19         {
20             if (int(T[i][lenT[i]])<97)
21                 T[i][lenT[i]]=char(int(T[i][lenT[i]])+32);
22             lenT[i]++;  
23         }
24     }
25     
26     for (int i=1;i<=M;i++){
27         scanf("%s",P[i]);
28         while(P[i][lenP[i]])
29         {
30             if(int(P[i][lenP[i]])<97)
31                 P[i][lenP[i]]=char(int(P[i][lenP[i]])+32);
32             lenP[i]++;
33         }
34     }
35     
36     for(int i=1;i<=N;i++){
37         int ANS=0;
38         for(int j=1;j<=M;j++){
39             int now=0;
40             for(int k=0;k<=lenT[i]-1;k++){
41                 if(T[i][k]==P[j][now]){
42                     now++;
43                     if(now==lenP[j]){
44                         ANS++;
45                         break;
46                     } 
47                 }
48             }
49         
50         }
51         cout<<ANS<<endl;
52     }
53     
54     return 0;
55 }

 

posted @ 2015-08-06 13:18  CXCXCXC  阅读(357)  评论(0编辑  收藏  举报