http://www.lydsy.com/JudgeOnline/problem.php?id=1622

这题我搜的题解是dp,我也觉得是dp,但是好像比模拟慢啊!!!!

1400ms不科学!

设f[i][j]为名字i位置的j字母最早出现的位置(向后)

f[i][j]=f[i+1][j]

f[i][a[i+1]]=i+1

那么就可以递推出,然后查找即可。。

但是大量的memcpy导致很慢啊。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005;
int f[N][265], n, m, ans;
char a[N][N], md[105][40]; int main() {
read(n); read(m);
for1(i, 1, n) { scanf("%s", a[i]+1); for1(j, 1, strlen(a[i]+1)-1) if(a[i][j]>='A'&&a[i][j]<='Z') a[i][j]=a[i][j]-'A'+'a'; }
for1(i, 1, m) { scanf("%s", md[i]); for1(j, 0, strlen(md[i])-1) if(md[i][j]>='A'&&md[i][j]<='Z') md[i][j]=md[i][j]-'A'+'a'; }
for1(i, 1, n) {
CC(f, 0);
for3(j, strlen(a[i]+1)-1, 0) {
memcpy(f[j], f[j+1], sizeof(f[j]));
f[j][(int)a[i][j+1]]=j+1;
}
ans=m;
for1(j, 1, m) {
int t=0;
for2(k, 0, strlen(md[j])) {
t=f[t][(int)md[j][k]];
if(!t) { --ans; break; }
}
}
printf("%d\n", ans);
}
return 0;
}

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

【BZOJ】1622: [Usaco2008 Open]Word Power 名字的能量(dp/-模拟)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草( dp )

    -------------------------------------------------------------------- #include<cstdio> #include ...

  8. BZOJ 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛( dp )

    一道水 dp ...然后我一开始用 BFS ...结果 MLE 了... dp[ i ][ j ][ k ] 由它四个方向上的 k - 1 转移. -------------------------- ...

  9. BZOJ 1592: [Usaco2008 Feb]Making the Grade 路面修整( dp )

    最优的做法最后路面的高度一定是原来某一路面的高度. dp(x, t) = min{ dp(x - 1, k) } + | H[x] - h(t) | ( 1 <= k <= t ) 表示前 ...

随机推荐

  1. PotPlayer 进度条显示缩略图

      PotPlayer设置鼠标放在播放器进度条任意位置显示缩略图 迁移时间--2017年8月9日15:41:27Author:Marydon 右键-->选项(F5)-->点击左侧面板上的“ ...

  2. 关于为什么要在项目中使用FTP文件服务器

    传统的上传一般做法是http上传,后台接收文件流,然后写入到服务器本地硬盘的某个位置. 如果我们想把文件单独存放在别的服务器上,那就可以借助ftp服务器了. 上传的流程则变为,http上传,后台接收文 ...

  3. 【TP5.0】引入public/static目录下的静态资源

    1.假设tp5 下只有一个项目,且application在tp5下, 2.我们知道 外界访问tp5项目,只能通过public/index.php的入口文件,且查看apache服务器我们知道, [DOC ...

  4. 【Redis】windows环境下安装redis服务器,并配置php的redis扩展

    win7示例: 1.下载Redis服务器 : https://github.com/dmajkic/redis/downloads:(随便下,建议不要太老的) 2.在D:\phpStudy\ 新建Re ...

  5. sql 百分比

    select [city], bfb=cast(cast(count(*)*100./(select count(*) from [UserBasicInfo]) as decimal(10,0)) ...

  6. kong 插件开发分析

    1.安装开发环境:(我这里用IntelliJ IDEA) 先安装lua 5.1和luarocks 因为kong基于openresty,openresty使用luajit luajit支持的是lua5. ...

  7. Python 爬虫实例(2)—— 爬取今日头条

    # coding:utf-8 import base64 import random, re import sqlite3 import redis, pickle import json, time ...

  8. 邁向 RHCE 之路 (Day26) - Apache 網頁伺服器

    本篇將在 SELinux 安全機制及 IPTables 防火牆開啟的環境下實作,分別實作簡單網頁服務及虛擬主機 Virtual Host 設定,最後則是實作網頁中需要保護網頁時可以透過 .htacce ...

  9. ie,cookie,域名与下划线

    前几天为了开发方便,和一个同事将XX.qq.com的测试站点拆成两个站点,我那个叫XX_hanks.qq.com,进行功能开发,在调试 cookie功能的时候,发现IE下cookie没有记住,用htt ...

  10. spring容器ApplicationContext初始化(spring应用上下文初始化)

    可以通过以下三种方式加载spring容器,实现bean的扫描与管理: 1. ClassPathXmlApplicationContext:从类路径中加载 ClassPathXmlApplication ...