BZOJ2061 : Country
记忆化搜索,设$f[i][j]$表示符号$i$一开始kmp指针为$j$,中间匹配了多少次,$g[i][j]$则表示匹配结束后kmp指针的位置。
时间复杂度$O(nl^2)$。
#include<cstdio>
#include<cstring>
const int N=26,M=105,P=10000;
int n,m,F,i,j,len[N],nxt[M],f[N][M],g[N][M],v[N][M];char s[M],a[N][M];
inline void up(int&x,int y){x+=y;if(x>=P)x-=P;}
void dp(int x,int y){
if(v[x][y])return;
int i,j;
for(i=0,j=y;i<len[x];i++)if(a[x][i]>='a'){
while(j&&s[j+1]!=a[x][i])j=nxt[j];
if(s[j+1]==a[x][i])j++;
if(j==m)up(f[x][y],1),j=nxt[j];
}else{
int k=a[x][i]-'A';
dp(k,j),up(f[x][y],f[k][j]),j=g[k][j];
}
v[x][y]=1,g[x][y]=j;
}
int main(){
scanf("%d%s",&n,s),F=s[0]-'A';
while(n--){
scanf("%s",s),m=strlen(s);
len[i=s[0]-'A']=m-2;
for(j=2;j<m;j++)a[i][j-2]=s[j];
}
scanf("%s",s+1),m=strlen(s+1);
for(i=2;i<=m;nxt[i++]=j){
while(j&&s[j+1]!=s[i])j=nxt[j];
if(s[j+1]==s[i])j++;
}
dp(F,0);
return printf("%d",f[F][0]),0;
}
BZOJ2061 : Country的更多相关文章
- wifi的country code
转自:http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.htmlCountry A 2 A 3 Number ------------- ...
- ural 1073. Square Country
1073. Square Country Time limit: 1.0 secondMemory limit: 64 MB There live square people in a square ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- 01背包 URAL 1073 Square Country
题目传送门 /* 题意:问n最少能是几个数的平方和 01背包:j*j的土地买不买的问题 详细解释:http://www.cnblogs.com/vongang/archive/2011/10/07/2 ...
- 计算机学院大学生程序设计竞赛(2015’12)The Country List
The Country List Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- [转]How to convert IP address to country name
本文转自:http://www.codeproject.com/Articles/28363/How-to-convert-IP-address-to-country-name Download ...
- Zoj3332-Strange Country II(有向竞赛图)
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...
随机推荐
- 第二课 less的学习以及移动端需要注意的问题
一.LESS的学习笔记: 1.less介绍:一种动态样式语言.less将css赋予了动态语言的特性,如变量,继承,运算,函数,less既可以在客户端上运行(支持IE6+,webkit,firefox) ...
- 《C#本质论》读书笔记(12)委托和Lambda表达式
12.1.委托概述 12.1.2 委托的数据类型 为了减少重复代码数量,可以将比较方法作为参数传递给 BubbleSort()方法.此外,为了将方法作为参数传递,必须有一个能够标识方法的数据类型--也 ...
- 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载
参考地址:http://www.cnblogs.com/zhili/p/4039111.html 一.如何在Windows Services中寄宿WCF服务 第一步:创建Windows 服务项目,具体 ...
- Arch Linux 安装、配置、美化和优化
国庆假期玩了下Arch Linux,发现这货跟Ubuntu之流相差甚远,甚难调教,而且安裝过程全命令行,会有各种问题,各种知识... --- 安装引导器--- -------------------- ...
- 运维自动化之ansible的安装与使用(包括模块与playbook使用)(转发)
原文 http://dl528888.blog.51cto.com/2382721/1435415 我使用过puppet(地址是http://dl528888.blog.51cto.com/2382 ...
- Visual Studio Code 1.0发布:100+语言,300+pull请求,1000+扩展
在第一个预览版发布一年后,微软发表了Visual Studio Code 1.0. 在//BUILD 2015大会上,微软宣布,他们的一个团队需要几个月来创建Visual Studio Code的第一 ...
- go sample - hello world
入门级别的hello world package mainimport "fmt"func main() { fmt.Println("blibliblbibl!&quo ...
- 2016北京网络赛 hihocoder 1391 Countries 树状数组
Countries 描述 There are two antagonistic countries, country A and country B. They are in a war, and ...
- 数据分析(4):Scipy
科学计算 最小二乘leastsq # -*- coding: utf-8 -*- def func(x,p): # p 参数列表 A,k,theta = p; # 可以一一对应赋值 return A* ...
- c++ 左值 和 右值
什么是lvalue, 什么是rvalue? lvalue: 具有存储性质的对象,即lvalue对象,是指要实际占用内存空间.有内存地址的那些实体对象,例如:变量(variables).函数.函数指针等 ...