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

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

else usaco 沒有 www. 有一個 line arm order created

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;
}

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