zoukankan      html  css  js  c++  java
  • 洛谷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".

    ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

    一开始我的想法是四重循环,第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 }
    代码
  • 相关阅读:
    vscode 代码补全工具之aiXcoder
    SQL语句替换某个字段的部分数据
    Antd中,Select 中value设值,导致placeholder不生效解决方法
    Git简易使用教程
    Hyper-V虚拟机上安装Ubuntu16.04/Ubuntu18.04.2LTS,搭建GitLab
    Hyper-V虚拟机安装Ubuntu,启动的时候会出现:Please remove the installation medium,then press ENTER
    来博客园9年多了,mark一下
    一步一步教你用IntelliJ IDEA 搭建SSM框架(3)——实现用户登录功能
    一步一步教你用IntelliJ IDEA 搭建SSM框架(2)——配置mybatis-geneator
    ITSEC TEAM 2013培训公开视频
  • 原文地址:https://www.cnblogs.com/frank06/p/10327695.html
Copyright © 2011-2022 走看看