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。。。。

  先来个超时的。。。

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
int N,M;
char T[][];
char P[][];
int lenT[];
int lenP[];
int main(){ cin>>N>>M;
for(int i=;i<=N;i++) scanf("%s",T[i]);
for(int i=;i<=M;i++) scanf("%s",P[i]); for(int i=;i<=N;i++){
for(int j=;j<=strlen(T[i])-;j++){
if(int(T[i][j])>=)
T[i][j]=char(int(T[i][j])-);
}
} for(int i=;i<=M;i++){
for(int j=;j<=strlen(P[i])-;j++){
if(int(P[i][j])>=)
P[i][j]=char(int(P[i][j])-);
}
} for(int i=;i<=N;i++){
int ANS=;
for(int j=;j<=M;j++){
int now=;
int len=strlen(P[j]);
for(int k=;k<=strlen(T[i])-;k++){
if(T[i][k]==P[j][now]){
now++;
if(now==len){
ANS++;
break;
}
}
} }
cout<<ANS<<endl;
} return ;
}

 

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

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
int N,M;
char T[][];
char P[][];
int lenT[];
int lenP[];
int main(){ cin>>N>>M; for (int i=;i<=N;i++){
scanf("%s",T[i]);
while (T[i][lenT[i]])
{
if (int(T[i][lenT[i]])<)
T[i][lenT[i]]=char(int(T[i][lenT[i]])+);
lenT[i]++;
}
} for (int i=;i<=M;i++){
scanf("%s",P[i]);
while(P[i][lenP[i]])
{
if(int(P[i][lenP[i]])<)
P[i][lenP[i]]=char(int(P[i][lenP[i]])+);
lenP[i]++;
}
} for(int i=;i<=N;i++){
int ANS=;
for(int j=;j<=M;j++){
int now=;
for(int k=;k<=lenT[i]-;k++){
if(T[i][k]==P[j][now]){
now++;
if(now==lenP[j]){
ANS++;
break;
}
}
} }
cout<<ANS<<endl;
} return ;
}

[Usaco2008 Open]Word Power 名字的能量的更多相关文章

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

    题目 1622: [Usaco2008 Open]Word Power 名字的能量 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 349  Solved ...

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

    1622: [Usaco2008 Open]Word Power 名字的能量 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 370  Solved: 18 ...

  3. bzoj1622 [Usaco2008 Open]Word Power 名字的能量

    Description     约翰想要计算他那N(1≤N≤1000)只奶牛的名字的能量.每只奶牛的名字由不超过1000个字待构成,没有一个名字是空字体串,  约翰有一张“能量字符串表”,上面有M(1 ...

  4. 【BZOJ】1622: [Usaco2008 Open]Word Power 名字的能量(dp/-模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1622 这题我搜的题解是dp,我也觉得是dp,但是好像比模拟慢啊!!!! 1400ms不科学! 设f[ ...

  5. BZOJ——1622: [Usaco2008 Open]Word Power 名字的能量

    http://www.lydsy.com/JudgeOnline/problem.php?id=1622 Description     约翰想要计算他那N(1≤N≤1000)只奶牛的名字的能量.每只 ...

  6. bzoj 1622: [Usaco2008 Open]Word Power 名字的能量【模拟】

    模拟即可,注意包含可以是不连续的 方便起见读入的时候全转成小写 #include<iostream> #include<cstdio> using namespace std; ...

  7. BZOJ_1622_[Usaco2008_Open]_Word_Power_名字的能量_(字符匹配_暴力)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1622 给出多个文本串和模式串,求每个文本串中有多少模式串. 分析 直接暴力... #inclu ...

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

    P2908 [USACO08OPEN]文字的力量Word Power 题目描述 Farmer John wants to evaluate the quality of the names of hi ...

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

    P2908 [USACO08OPEN]文字的力量Word Power 题目描述 Farmer John wants to evaluate the quality of the names of hi ...

随机推荐

  1. WPF DataGrid 获取选中的当前行某列值

    方法一: DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; ]ToString(); 方法二: var a = ...

  2. Ubuntu 16.04 LTS sublime text 3 解决不能输入中文

    sublime text 3 安装完成后不能输入中文,让人很是不爽.下面内容可以解决使用问题! 一.首先要注意几个问题. 1)sublime_imfix.c 文件放在home目录下面. 2)如果你在步 ...

  3. Ruby on Rails 初次冲浪体验

    为了更好的阅读体验,欢迎訪问 作者博客原文 Rails is a web application development framework written in the Ruby language. ...

  4. 【BZOJ4504】K个串 可持久化线段树+堆

    [BZOJ4504]K个串 Description 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只被统计 ...

  5. English Grammar

    What is Grammar?  

  6. HTML Colors

    https://www.w3schools.com/html/html_colors.asp <!DOCTYPE html> <html> <body> #169f ...

  7. filezilla 读取目录失败

    用到FTP,本来一直用主动模式,可以最近老是读取目录失败,425 Can't open data connection 和 读取目录列表失败(搞了好久,一天) 问题解决 这个问题主要是由于使用Pass ...

  8. python基础之类的封装

    从封装本身的意思去理解,封装就好像是拿来一个麻袋,把小猫,小狗,小王八,还有alex一起装进麻袋,然后把麻袋封上口子.但其实这种理解相当片面 一 封装什么 你钱包的有多少钱(数据的封装) 你的性取向( ...

  9. 【Linux command reference】

    ubuntu16.04安装中文输入法: https://blog.csdn.net/singleyellow/article/details/77448246 ubuntu16.04 用vi编辑代码, ...

  10. Linux bridge 资料链接

    1.Performance Evalution of Linux Bridge https://tnt.aufbix.org/_media/linux/yu-linux-tsm2004.pdf 2.L ...