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 ...
随机推荐
- SQL记录-PLSQL面向对象
PL/SQL面向对象 PL/SQL允许定义一个对象类型,这有助于在Oracle的数据库中设计的面向对象.对象类型可以包装复合类型.使用对象允许实现数据的具体结构现实世界中的对象和方法操作它.对象有属性 ...
- 数学:拓展Lucas定理
拓展Lucas定理解决大组合数取模并且模数为任意数的情况 大概的思路是把模数用唯一分解定理拆开之后然后去做 然后要解决的一个子问题是求模质数的k次方 将分母部分转化成逆元再去做就好了 这里贴一份别人的 ...
- SCI写作经典替换词
- 第7月第19天 swift on linux
1. https://github.com/iachievedit/moreswift http://dev.iachieved.it/iachievedit/more-swift-on-linux/ ...
- 洛谷 P3916 【图的遍历】反向加边+dfs
前言: 对于这类带环的图,一般记忆化搜索不能很好的对所有遍历的边进行更新取值.因为环上的点可以相互到达,所以他们的答案因当是同步更新的,而dfs一旦你回溯完环上某个点就不会在更新这个点的答案了,做不到 ...
- LeetCode-Valid Number - 有限状态机
判断合法数字,之前好像在哪里看到过这题, 记得当时还写了好久,反正各种改, 今天看到了大神的解法(https://github.com/fuwutu/LeetCode/blob/master/Vali ...
- 关于golang的defer的练习
golang的defer怎么说.大意就是在函数return后.函数关闭前.按照filo的顺序来执行的关键字 上代码: package main import ( "fmt" ) f ...
- 通俗理解决策树中的熵&条件熵&信息增益
参考通俗理解决策树算法中的信息增益 说到决策树就要知道如下概念: 熵:表示一个随机变量的复杂性或者不确定性. 假如双十一我要剁手买一件衣服,但是我一直犹豫着要不要买,我决定买这件事的不确定性(熵)为2 ...
- php中数据库连接方式pdo和mysqli对比分析
1)总的比较 PDO MySQLi 数据库支持 12种不同的数据库支持 支持MySQL API OOP OOP + 过程 Connection Easy Easy 命名参数 支持 不支持 对象映射 ...
- fedroa20中将ssh,ssl升级到当前最新版本
Fedroa20下手工安装openssh-server 本例以Fedroa20为例,需要下载:zlib-1.2.8.tar.openssl-1.0.2e.tar. openssh-7.1p1.tar. ...