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;
}

  

时间: 2024-11-05 03:12:07

BZOJ2911 : [Poi1997]The Number of Symmetrical Choices的相关文章

POJ1143:Number Game(状态压缩)

Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take turns choosing integers greater than 1. First, Christine chooses a number, then Matt chooses a nu

Number Game poj1143

Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take turns choosing integers greater than 1. First, Christine chooses a number, then Matt chooses a nu

Number Game_状态压缩

Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take turns choosing integers greater than 1. First, Christine chooses a number, then Matt chooses a nu

排列&组合

What's the Difference? In English we use the word "combination" loosely, without thinking if the order of things is important. In other words: "My fruit salad is a combination of apples, grapes and bananas" We don't care what order the

POJ 1143 记忆化搜索+博弈论

Number Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3181   Accepted: 1280 Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take tu

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

Pku 1143: Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take turns choosing integers greater than 1. First, Christine chooses a number, then Matt ch

hdu1143 状态压缩dp 记忆化搜索写法

http://poj.org/problem?id=1143 Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take turns choosing integers greater than 1. First, Christine chooses a

Usage of “symmetrical” and “symmetric”

What is the appropriate usage of "symmetrical" and "symmetric" (using the geometrical adjectival definition of both terms)? Are they synonymous? Dictionary: Merriam-Webster lists symmetric as being a variant of symmetrical, which is th

choices

1.定义. /** * \brief %Choice for performing commit * * Must be refined by inheritance such that the information stored * inside a choice is sufficient to redo a commit performed by a * particular brancher. * * \ingroup TaskActor */ class GECODE_VTABLE_