新建源汇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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. Number Game_状态压缩

    Description Christine and Matt are playing an exciting game they just invented: the Number Game. The ...

  3. Number Game poj1143

    Description Christine and Matt are playing an exciting game they just invented: the Number Game. The ...

  4. [POJ1143]Number Game

    [POJ1143]Number Game 试题描述 Christine and Matt are playing an exciting game they just invented: the Nu ...

  5. apex-utility-ai-unity-survival-shooter

    The AI has the following actions available: Action Function Shoot Fires the Kalashnikov Reload Reloa ...

  6. 《ACM国际大学生程序设计竞赛题解I》——6.10

    Pku 1143: Description Christine and Matt are playing an exciting game they just invented: the Number ...

  7. ACM学习-POJ-1143-Number Game

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1143-Number Game Number Game Time Limit: 1000MS   Memory ...

  8. Python3选择支持非ASCII码标识符的缘由

    原文在: PEP 3131 -- Supporting Non-ASCII Identifiers. Python2并不支持非ASCII码标识符. PEP的全称是Python Enhancement ...

  9. POJ-1143(状态压缩)

    Number Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3432 Accepted: 1399 Descripti ...

随机推荐

  1. CSS变量试玩儿

    CSS很美妙,能够为您的页面穿上衣裳,各种各样五彩斑斓的衣裳,但是对于开发者来说,他又不够灵动,于是乎有了各种各样的预处理器Sass.LESS.Stylus(笔者建议Sass的SCSS语法),这些预处 ...

  2. CF #442 div2

    A 判断下5个名字出现了几次.pre数据巨弱,就这么一水题在std测刷掉了非常多的人.. /** @Date : 2017-10-24 16:04:41 * @FileName: A.cpp * @P ...

  3. javascript沙箱模式

    沙箱模式解决了命名空间模式的如下几个缺点: 1.对单个全局变量的依赖变成了应用程序的全局变量依赖.在命名空间模式中,是没有办法使同一个应用程序或库的2个版本运行在同一个页面中.2.对这种以点分割的名字 ...

  4. header()跳转

    if ($toNews == 1) { header('Location:/ucenter/pageMailBox/2'); exit; } PHP跳转页面,用 header() 函数 定义和用法 h ...

  5. 记录下(同一个计算机)多个容器 dockr bridge(桥接) docker-compose 配置

    直接上 version: '3' services: mysql: container_name: mysql image: mysql:5.7.21 environment: MYSQL_ROOT_ ...

  6. python垃圾回收三之标记清除

    #第一组循环引用# a = [1,2] b = [3,4] a.append(b) b.append(a) del a ## #第二组循环引用# c = [4,5] d = [5,6] c.appen ...

  7. Javascript - 预编译与函数词法作用域

    预编译与函数词法作用域(Precompiled & Scoped) 预编译 Javascript脚本的宿主在执行代码之前对脚本做了预编译处理,比如浏览器对Js进行了预编译,编译器会扫描所有的声 ...

  8. linux 查看有哪些service

    一.利用进程来查看命令里 ps -aux | grep xxx 是查看某个进程或者服务是否存在.二.利用chkconfig配置工具chkconfig --list 可以列出所有的服务在各个runlev ...

  9. Linux Core Dump【转】

    转自:http://www.cnblogs.com/hazir/p/linxu_core_dump.html 当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内存状态记录下来,保存在一个文件中 ...

  10. CasperJS API中文博客链接

    http://www.cnblogs.com/reach296/tag/Casperjs/