新建源汇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. bzoj千题计划240:bzoj3900: 交换茸角

    http://www.lydsy.com/JudgeOnline/problem.php?id=3900 dp[i]表示让状态为i的鹿满足要求的最少交换次数 不能枚举两头鹿交换,因为一头鹿可能交换多次 ...

  2. POJ 3537 multi-sg 暴力求SG

    长为n的一列格子,轮流放同种棋子,率先使棋子连成3个者胜. 可以发现每次放一个棋子后,后手都不能放在[x-2,x+2]这个区间,那么相当于每次放棋将游戏分成了两个,不能放棋者败. 暴力求SG即可 /* ...

  3. 安装mongodb以及设置为windows服务 详细步骤

    我的win7 32的,注意版本要正确! 一.下载mongodb压缩包:mongodb-win32-i386-2.6.9.zip() 二.在D盘新建文件夹mongodb,将压缩我的解压文件放进去(有一个 ...

  4. mac 无法验证副本

    转: 这个是拆机后断了电源,导致时间不对,也就是说现在电脑的时间比U盘制作的时间还早,所以有这样的错误提示. 在终端里面修改时间请参考下面的代码,按回车键确认:date 062614102014.30 ...

  5. BFS的队列

    按老师上课的话来总结,队列变化多端:   普通模板没有代价: 普通队列FIFO 01代价: 双端队列,单调队列 任意代价: 优先队列/堆,最短路SPFA/DIJKSTRA

  6. MySQL-数据操作-增删改查

    1.增加: insert into 表 (列名,列名...) values (值,值,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值.. ...

  7. PWN入门

    pwn ”Pwn”是一个黑客语法的俚语词 ,是指攻破设备或者系统 .发音类似“砰”,对黑客而言,这就是成功实施黑客攻击的声音——砰的一声,被“黑”的电脑或手机就被你操纵.以上是从百度百科上面抄的简介, ...

  8. 【干货】查看windows文件系统中的数据—利用簇号查看文件与恢复文件

    前面我们使用这个软件发现了很多删除掉的数据,今天来看看簇.FAT文件系统中,存在一个簇的链接,我知道了簇1在哪里就可以顺藤摸瓜恢复所有的信息. 这里使用FAT 12为例子,FAT其他万变不离其宗,甚至 ...

  9. Oracle 数据库和监听器开机自启动两种实现方法

    数据库和监听器开机自启动   编辑oratab文件: 修改:orcl:/u01/app/oracle/product/11.2.0/db_1:N            orcl:/u01/app/or ...

  10. js array数组检测方式

    typeof var arr = [1,2,3]; console.log(typeof arr);//'object' 数组的本质是一种特殊的对象,所以返回'object'.typeof运算符只能用 ...