链接

因为有交换相邻字母,因此给你字符串就相当于给你了这个字符串的所有排列

把等价的串映射到整数范围,再根据 \(m\) 种魔法连边,缩点后在 DAG 上DP即可

无耻地用了int128

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
#define dbg(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
typedef __int128 ll;
typedef unsigned int uint;
typedef unsigned long long ull;
template<typename T,typename U>inline char smin(T&x,const U&y){return x>y?x=y,1:0;}
template<typename T,typename U>inline char smax(T&x,const U&y){return x<y?x=y,1:0;}
const int N=52,MN=N*N*N;
int n,m,c1[N<<1][4],c2[N<<1][4],num[N][N][N],T;
ll C[N][N],cnt[MN],val[MN],f[MN],ans;
char s1[N],s2[N];
int head[MN],fr[MN*N],to[MN*N],nxt[MN*N],tot;
inline void add(int x,int y){if(x==y)return;fr[++tot]=x,to[tot]=y,nxt[tot]=head[x];head[x]=tot;}
int s[MN],top,dfn[MN],dfs_clock,bel[MN],scc;
bool ins[MN];
int dfs(int x){
int low=dfn[x]=++dfs_clock;
s[++top]=x,ins[x]=1;
for(int i=head[x];i;i=nxt[i]){
const int&y=to[i];
if(!dfn[y])smin(low,dfs(y));
else if(ins[y])smin(low,dfn[y]);
}
if(low==dfn[x]){
++scc;
do bel[s[top]]=scc,val[scc]+=cnt[s[top]],ins[s[top]]=0;while(s[top--]!=x);
}
return low;
}
int ind[MN],q[MN];
void toposort(){
int l=1,r=0,x;
REP(i,1,scc)if(!ind[i])q[++r]=i;
while(l<=r){
x=q[l++];f[x]+=val[x];smax(ans,f[x]);
for(int i=head[x];i;i=nxt[i]){
smax(f[to[i]],f[x]);
if(!--ind[to[i]])q[++r]=to[i];
}
}
}
template<typename Tp>inline void print(Tp x){
static char s[50];
int top=0;
for(;x;x/=10)s[++top]=x%10^'0';
while(top)putchar(s[top--]);
}
int main(){
scanf("%d%d",&n,&m);
REP(i,1,m){
scanf("%s%s",s1+1,s2+1);int len=strlen(s1+1);
REP(j,1,len)++c1[i][s1[j]-'A'],++c2[i][s2[j]-'A'];
}
C[0][0]=1;
REP(i,1,n){
C[i][0]=1;
REP(j,1,i)C[i][j]=C[i-1][j]+C[i-1][j-1];
}
REP(i,0,n)REP(j,0,n-i)REP(k,0,n-i-j){
num[i][j][k]=++T;
cnt[T]=C[n][i]*C[n-i][j]*C[n-i-j][k];
}
REP(a,0,n)REP(b,0,n-a)REP(c,0,n-a-b){
int d=n-a-b-c;
REP(i,1,m)if(a>=c1[i][0]&&b>=c1[i][1]&&c>=c1[i][2]&&d>=c1[i][3]){
int e,f,g,h;
e=a-c1[i][0]+c2[i][0];
f=b-c1[i][1]+c2[i][1];
g=c-c1[i][2]+c2[i][2];
add(num[a][b][c],num[e][f][g]);
}
}
REP(i,1,T)if(!dfn[i])dfs(i);
int all=tot;tot=0;memset(head,0,sizeof head);
REP(i,1,all){
int&bx=bel[fr[i]],&by=bel[to[i]];
if(bx!=by)add(bx,by),++ind[by];
}
toposort();print(ans);
return 0;
}

[NOI.AC#35]string 缩点+拓扑排序的更多相关文章

  1. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

  2. [HAOI2006]受欢迎的牛 tarjan缩点 + 拓扑排序

    ---题面--- 题解: 首先tarjan缩点应该还是容易想到的,因为喜爱具有传递性,所以一个强联通分量里面的点实际上是全部等效的,所以我们可以缩成一个方便判断, 缩完点之后整张图就变成了一个有向无环 ...

  3. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812 ...

  4. hdu 1811(缩点+拓扑排序+并查集)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  6. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  7. POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

  8. poj 2762 强连通缩点+拓扑排序

    这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...

  9. 2018.11.06 bzoj1093: [ZJOI2007]最大半连通子图(缩点+拓扑排序)

    传送门 先将原图缩点,缩掉之后的点权就是连通块大小. 然后用拓扑排序统计最长链数就行了. 自己yyyyyy了一下一个好一点的统计方法. 把所有缩了之后的点都连向一个虚点. 然后再跑拓扑,这样最后虚点的 ...

随机推荐

  1. Unity Shader (四)片段程序示例

      1.环境光+漫反射+高光+点光源 Shader "Custom/Example_Frag_1" { properties { _MainColor(,,,) _Specular ...

  2. DATA_PUMP_DIR impdp 指定导出目录

    1.mkdir /tdms1/oracle/dump 2.sqlplus / as sysdba 3.create directory udir as '/tdms1/oracle/dump'; 4. ...

  3. django-xadmin使用之更改菜单url

    环境:xadmin-for-python3 python3.5.2 django1.9.12 1. 在模块的adminx.py文件中增加以下代码: class AdminSettings(object ...

  4. KM最大匹配 HDU 2255

    KM算法详解+模板 - wenr - 博客园  http://www.cnblogs.com/wenruo/p/5264235.html #include<iostream> #inclu ...

  5. 进程:linux用户态-内核态

    用户态:Ring3运行于用户态的代码则要受到处理器的诸多检查,它们只能访问映射其地址空间的页表项中规定的在用户态下可访问页面的虚拟地址,且只能对任务状态段(TSS)中I/O许可位图(I/O Permi ...

  6. [Recompose] Configure Recompose to Build React Components from RxJS Streams

    Recompose provides helper functions to stream props using an Observable library of your choice into ...

  7. POJ2485 Highways 【MST】

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22842   Accepted: 10525 Descri ...

  8. shrio int配置

    之前章节我们已经接触过一些INI配置规则了,如果大家使用过如Spring之类的IoC/DI容器的话,Shiro提供的INI配置也是非常类似的,即可以理解为是一个IoC/DI容器,但是区别在于它从一个根 ...

  9. MySql免安装版绿化版安装配置,附MySQL服务无法启动解决方案

    整理于:https://www.cnblogs.com/cenwei/p/6249856.html      我下载的MySQL版本是:mysql-5.6.15-winx64 一.解压文件 下载好My ...

  10. MySQL Server 5.5.44免安装版配置详解

    转载地址:http://wenku.baidu.com/view/2a8bfe6a25c52cc58bd6beff.html### 一 下载MySQL http://dev.mysql.com/dow ...