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

题目描述

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".

思路:暴力

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int ans[1001];
char c[31],s[1001][1010];
bool judge(int pos,int j,int k){
    if(c[pos]>='A'&&c[pos]<='Z')    c[pos]+=32;
    if(s[j][k]>='A'&&s[j][k]<='Z')    s[j][k]+=32;
    if(c[pos]==s[j][k])    return true;
    else return false;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%s",s[i]);
    for(int i=1;i<=m;i++){
        scanf("%s",c);
        for(int j=1;j<=n;j++){
            int len=strlen(s[j]);
            int pos=0;
            for(int k=0;k<len;k++)
                if(judge(pos,j,k))    pos++;
            if(pos==strlen(c))    ans[j]++;
        }
    }
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<endl;
}

 

posted @ 2017-11-16 21:55  一蓑烟雨任生平  阅读(240)  评论(0编辑  收藏  举报