BZOJ2911 : [Poi1997]The Number of Symmetrical Choices
新建源汇S,T,根据题意可以建出一个DAG
设f[x][y]为从x走到y的回文路径的方案数,则
边界条件:
f[x][x]=1
对于一条边x->y,若a[x]==a[y],则f[x][y]=1
转移方程为:
若a[x]==a[y],则f[x][y]=sum(f[i][j])(x->i有边,j->y有边)
若a[x]!=a[y],则f[x][y]=0
最终答案即为f[S][T],时间复杂度$O(L^2)$。
#include<cstdio>
#include<cstring>
#define N 410
int n,m,i,j,k,l,a[N],st[2][N],en[2][N],v[N][N],f[N][N];char s[N];
struct E{int v;E*nxt;}*g[N],*h[N],pool[2000],*cur=pool;
inline void add(int x,int y){
E*p=cur++;p->v=y;p->nxt=g[x];g[x]=p;
p=cur++;p->v=x;p->nxt=h[y];h[y]=p;
}
int F(int x,int y){
if(v[x][y])return f[x][y];
v[x][y]=1;
if(a[x]!=a[y])return 0;
for(E*p=g[x];p;p=p->nxt)for(E*q=h[y];q;q=q->nxt)f[x][y]+=F(p->v,q->v);
return f[x][y];
}
int main(){
scanf("%d",&n),m=2;
for(k=0;k<2;k++)for(i=1;i<=n;i++){
st[k][i]=m+1;
for(scanf("%s",s+1),l=std::strlen(s+1),j=1;j<l;j++)add(m+j,m+j+1);
for(j=1;j<=l;j++)a[m+j]=s[j]-'a'+1;
en[k][i]=m+=l;
}
for(i=1;i<n;i++){
add(en[0][i],st[0][i+1]);
add(en[0][i],st[1][i+1]);
add(en[1][i],st[0][i+1]);
add(en[1][i],st[1][i+1]);
}
add(1,st[0][1]);
add(1,st[1][1]);
add(en[0][n],2);
add(en[1][n],2);
for(i=1;i<=m;i++){
v[i][i]=f[i][i]=1;
for(E*p=g[i];p;p=p->nxt){
v[i][p->v]=1;
if(a[i]==a[p->v])f[i][p->v]=1;
}
}
return printf("%d",F(1,2)),0;
}
BZOJ2911 : [Poi1997]The Number of Symmetrical Choices的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Number Game_状态压缩
Description Christine and Matt are playing an exciting game they just invented: the Number Game. The ...
- Number Game poj1143
Description Christine and Matt are playing an exciting game they just invented: the Number Game. The ...
- [POJ1143]Number Game
[POJ1143]Number Game 试题描述 Christine and Matt are playing an exciting game they just invented: the Nu ...
- apex-utility-ai-unity-survival-shooter
The AI has the following actions available: Action Function Shoot Fires the Kalashnikov Reload Reloa ...
- 《ACM国际大学生程序设计竞赛题解I》——6.10
Pku 1143: Description Christine and Matt are playing an exciting game they just invented: the Number ...
- ACM学习-POJ-1143-Number Game
菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1143-Number Game Number Game Time Limit: 1000MS Memory ...
- Python3选择支持非ASCII码标识符的缘由
原文在: PEP 3131 -- Supporting Non-ASCII Identifiers. Python2并不支持非ASCII码标识符. PEP的全称是Python Enhancement ...
- POJ-1143(状态压缩)
Number Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3432 Accepted: 1399 Descripti ...
随机推荐
- hdu 5290 Bombing plan
http://acm.hdu.edu.cn/showproblem.php?pid=5290 题意: 一棵树,每个点有一个权值wi,选择点i即可破坏所有距离点i<=wi的点,问破坏所有点 最少需 ...
- js调试系列: 初识控制台
写在最开头:其实我以前就在考虑要不要写这个东西,因为这个东西确实不难,但是为什么会有这么多人问,他们问的不是怎么用控制台,而是不知道控制台能干嘛,他们也知道有 console.log 之类的东西,但他 ...
- html5 canvas旋转
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [整理]WebAPP开发的框架
http://www.zhihu.com/question/27210335 http://ionicframework.com/getting-started/ http://cordova.apa ...
- OnContextMenu事件(转)
用oncontextmenu事件单禁用右键菜单 一个页面中,BODY中用oncontextmenu='return false'来取消鼠标右键:在JS中设置oncontextmenu='return ...
- Sublime Text 2 破解 on Mac
用Sublime Text 2自己打开自己的二进制文件:Sublime Text 2/Contents/MacOS/Sublime Text 2 搜索所有3342 3032 都替换成3242 3032 ...
- HDU 2112 HDU Today 最短路
题目描述: Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这 ...
- 在xampp与phpstorm环境下安装xdebug[转]
XDebug是什么 很多PHP程序员调试使用echo.print_r().var_dump().printf()等,虽然对于有较丰富开发经验的程序员来说这些也已经足够了,他们往往可以在程序执行的过程中 ...
- php递归函数细节
<?php /** *php递归函数细节 *从1到5的阶乘 * */ header("Content-Type:text/html;charset=utf-8"); func ...
- Scala工具库
1. Scala json解析库:https://github.com/json4s/json4s