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 ...
随机推荐
- MySql与对应的Java的时间类型
MySql的时间类型有 Java中与之对应的时间类型date java.sql.Date Date ...
- bzoj千题计划255:bzoj3572: [Hnoi2014]世界树
http://www.lydsy.com/JudgeOnline/problem.php?id=3572 明显需要构造虚树 点属于谁管理分三种情况: 1.属于虚树的点 2.在虚树上的边上的点 3.既不 ...
- ZSTU OJ 3770: 黑帽子 归纳总结
Description 一群非常聪明的人开舞会,每人头上都戴着一顶帽子.帽子只有黑白两种,黑的至少有一顶.每个人都能看到其它人帽子的颜色,却看不到自己的.主持人先让大家 看看别人头上戴的是什幺帽子,然 ...
- 利用 ASP.NET 的内置功能抵御 Web 攻击 (1)
摘要: Dino 总结了最常见的 Web 攻击类型,并介绍了 Web 开发人员可以如何使用 ASP.NET 的内置功能来改进安全性. 一.ASP.NET 开发人员应当始终坚持的做法 如果您正在阅读本文 ...
- swift相关文档
swift官方文档 swift官方文档 https://itunes.apple.com/cn/book/swift-programming-language/id881256329?mt=11 sw ...
- HDU 4506 小明系列故事——师兄帮帮忙(二分快速幂)
题意:就是输入一个数组,这个数组在不断滚动,而且每滚动一次后都要乘以一个数,用公式来说就是a[i] = a[i-1] * k;然后最后一位的滚动到第一位去. 解题报告:因为题目中的k要乘很多次,达到了 ...
- 第10月第20天 afnetwork like MKNetworkEngine http post
1. + (AFHTTPRequestOperation *)requestSellerWithCompletion:(requestFinishedCompletionBlock)successBl ...
- Bogus URL svn: is not properly URI-encoded
问题描述: 从浏览器地址栏复制出来的url 放到eclipse 的svn插件里,新建资源库位置 总是报错 Bogus URL svn: ********************** is not ...
- ps命令实用方法.ps -l ps -L详解
一.统计sleep状态的进程. c233 plugins # ps -elf|head -1F S UID PID PPID C PRI NI ADDR SZ WCHA ...
- kibana提示"[illegal_argument_exception] mapper [hits] cannot be changed from type [long] to [integer]"
=============================================== 2019/1/30_第1次修改 ccb_warlock == ...