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 ...
随机推荐
- 比较原声socket 、GCDAsyncSocket
原声socket NSInputStream 输入流(OC)NSOutputStream 输出流(OC)1:通过c语言的输入输出流CFReadStreamRef/CFWirteStreamRef(输入 ...
- android中点击空白处隐藏软键盘
InputMethodManager manager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERV ...
- 谈谈我的编程之路---WAMP(三)
WAMP的一些配置与使用心得(APACHE)说实话,我感觉apache的配置真的还是蛮复杂的,感觉好像又在学一种语言,让我用比较庞大的概念来讲述这些东西,我也没办法做到就以实际应用出发出发,简单的说一 ...
- SAE云平台上传图片和发送邮件
1.远程图片保存至Storage 其中public是Storage中的容器名,"目录1/目录2/"是容器下的路径 $file_content 是得到的文件数据 $s = new S ...
- Android -- Looper、Handler、MessageQueue等类之间关系的序列图
原文:Android源码解析之(二)-->异步消息机制 通过阅读文章及其中提到的一些参考文章,模模糊糊的理解了Android的异步消息机制.为了能够进行消化吸收,决定把各类之间的交互通过图的形式 ...
- [LeetCode] Pow(x, n)
Implement pow(x, n). 有史以来做过最简单的一题,大概用5分钟ac,我采用fast exponential,这个在sicp的第一章就有描述.思想是:如果n是偶数的话,那么m^n = ...
- java创建线程的几种方式
1.继承Thread类 /** * @author Ash * @date: 2016年8月6日 下午10:56:45 * @func: 通过继承Thread类来实现多线程 * @email 4086 ...
- C++中引用(&)的用法和应用实例
转自:http://www.cnblogs.com/Mr-xu/archive/2012/08/07/2626973.html 对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能 ...
- php获取当前毫秒时间戳
最近在做一个智能家居项目的后台,需要实时上传用户对智能设备的配置信息到服务器,以便实现同步,因此对于时间的精确度要求比较高,最开始直接是用php的time()函数来获取时间戳,获取的时间精确到秒级别, ...
- MFC GDI绘图基础
一.关于GDI的基本概念 什么是GDI? Windows绘图的实质就是利用Windows提供的图形设备接口GDI(Graphics Device Interface)将图形绘制在显示器上. 在Wind ...