1. 程式人生 > >洛谷P2908 [USACO08OPEN]文字的力量Word Power

洛谷P2908 [USACO08OPEN]文字的力量Word Power

with there 以及 等等 stream img each ets contain

題目描述

Farmer John wants to evaluate the quality of the names of his N (1 <= N <= 1000) cows. Each name is a string with no more than 1000 characters, all of which are non-blank.

He has created a set of M (1 <= M <= 100) ‘good‘ strings (no

longer than 30 characters and fully non-blank). If the sequence letters of a cow‘s name contains the letters of a ‘good‘ string in the correct order as a subsequence (i.e., not necessarily all next to each other), the cow‘s name gets 1 quality point.

All strings is case-insensitive, i.e., capital letters and lower case letters are considered equivalent. For example, the name ‘Bessie‘ contains the letters of ‘Be‘, ‘sI‘, ‘EE‘, and ‘Es‘ in the correct order, but not ‘is‘ or ‘eB‘. Help Farmer John determine the number of quality points in each of his cow‘s names.

約翰想要計算他那N(l < =N <= 1000)只奶牛的名字的能量.每只奶牛的名字由不超過1000個字 符構成,沒有一個名字是空字體串.

約翰有一張“能量字符串表”,上面有M(1 < =M < =100)個代表能量的字符串.每個字符串 由不超過30個字體構成,同樣不存在空字符串.一個奶牛的名字蘊含多少個能量字符串,這個名 字就有多少能量.所謂“蘊含”,是指某個能量字符串的所有字符都在名字串中按順序出現(不 一定一個緊接著一個).

所有的大寫字母和小寫字母都是等價的.比如,在貝茜的名字“Bessie”裏,蘊含有“Be” “si” “EE”以及“Es”等等字符串,但不蘊含“Ls”或“eB” .請幫約翰計算他的奶牛的名字 的能量.

輸入輸出格式

輸入格式:

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Line i+1 contains a string that is the name of the ith cow

* Lines N+2..N+M+1: Line N+i+1 contains the ith good string

輸出格式:

* Lines 1..N+1: Line i+1 contains the number of quality points of the ith name

輸入輸出樣例

輸入樣例#1: 復制
5 3 
Bessie 
Jonathan 
Montgomery 
Alicia 
Angola 
se 
nGo 
Ont 
輸出樣例#1: 復制
1 
1 
2 
0 
1 

說明

There are 5 cows, and their names are "Bessie", "Jonathan", "Montgomery", "Alicia", and "Angola". The 3 good strings are "se", "nGo", and "Ont".

"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層枚舉牛的名字中的每個字符,第3層枚舉能量字符串,第4層枚舉能量字符串中的每個字符,後來發現這個想法不對,正確的思路是最外層枚舉牛的名字,第2層枚舉能量字符串,最裏層枚舉能量字符串中的每個字符。如果枚舉的能量字符串裏的字符在牛的名字裏出現了,那麽枚舉下一個字符,當整個字符串全部枚舉完成時,代表這個奶牛的名字“蘊含”這個能量字符串,能量+1,循環完後輸出總能量就行了。

技術分享圖片
 1 #include<iostream>
 2 using namespace std;
 3 int n,m,s;
 4 string a[1001],b[101];
 5 int main()
 6 {
 7     cin>>n>>m;
 8     for(int i=1;i<=n;i++)
 9     {
10         cin>>a[i];
11         for(int j=0;j<a[i].length();j++)
12             if(a[i][j]>=A&&a[i][j]<=Z)
13                 a[i][j]+=32;
14     }
15     for(int i=1;i<=m;i++)
16     {
17         cin>>b[i];
18         for(int j=0;j<b[i].length();j++)
19             if(b[i][j]>=A&&b[i][j]<=Z)
20                 b[i][j]+=32;
21     }
22     for(int i=1;i<=n;i++)
23     {
24         s=0;
25         for(int j=1;j<=m;j++)
26         {
27             int l=0;
28             for(int k=0;k<a[i].length();k++)
29             {
30                 if(a[i][k]==b[j][l])
31                     l++;
32                 if(l==b[j].length())
33                 {
34                     s++;
35                     break;
36                 }
37             }
38         }
39         cout<<s<<endl;
40     }
41     return 0;
42 }
代碼

洛谷P2908 [USACO08OPEN]文字的力量Word Power