[TC11326]ImpossibleGame
[TC11326]ImpossibleGame
题目大意:
一类字符串仅由'A','B','C','D'四种字母组成。对于这样的一个字符串\(S\),可以用以下两种方式之一修改这个字符串:
- 交换\(S\)中的相邻两个字母;
- 使用一种魔法。魔法共\(m(m\le50)\)种,其中第\(i\)种可以将\(S\)中一个等于\(X_i\)的子串替换为\(y_i\),保证\(|X_i|=|Y_i|\)。
求对于所有这样的长度为\(n(n\le30)\)的字符串\(S\),修改过程中出现的不同的字符串最多能有多少?
思路:
对于第一种操作,显然若两个字符串中各字符出现次数对应相等,则这两个字符串之间可以相互转化。
因此我们可以用一个三元组\((a,b,c)\)表示字符串的状态,其中\(a,b,c\)分别表示字符'A','B','C'出现的次数。
魔法就相当于在这些状态间连边。
缩点后在DAG上DP即可。
时间复杂度\(\mathcal O(n^3m)\)
源代码:
#include<stack>
#include<queue>
#include<string>
#include<vector>
class ImpossibleGame {
private:
using int64=long long;
static constexpr int N=31,S=5457;
int64 fac[N],w[S],f[S];
int id[N][N][N],tot,a[S],b[S],c[S],d[S];
void init(const int &n) {
for(register int i=fac[0]=1;i<=n;i++) {
fac[i]=fac[i-1]*i;
}
for(register int i=0;i<=n;i++) {
for(register int j=0;i+j<=n;j++) {
for(register int k=0;i+j+k<=n;k++) {
const int l=n-i-j-k;
id[i][j][k]=++tot;
a[tot]=i;
b[tot]=j;
c[tot]=k;
d[tot]=l;
w[tot]=fac[n]/fac[i]/fac[j]/fac[k]/fac[l];
}
}
}
}
struct Edge {
int u,v;
};
std::vector<Edge> edge;
std::vector<int> e[S];
inline void add_edge(const int &u,const int &v) {
e[u].emplace_back(v);
}
bool ins[S];
std::stack<int> s;
int dfn[S],low[S],scc[S],ind[S];
void tarjan(const int &x) {
dfn[x]=low[x]=++dfn[0];
s.push(x);
ins[x]=true;
for(auto &y:e[x]) {
if(!dfn[y]) {
tarjan(y);
low[x]=std::min(low[x],low[y]);
} else if(ins[y]) {
low[x]=std::min(low[x],dfn[y]);
}
}
if(low[x]==dfn[x]) {
int y;
scc[0]++;
do {
y=s.top();
s.pop();
ins[y]=false;
scc[y]=scc[0];
f[scc[0]]+=w[y];
} while(y!=x);
}
}
std::queue<int> q;
int64 dis[S];
int64 kahn() {
for(register int i=1;i<=scc[0];i++) {
if(!ind[i]) {
dis[i]=f[i];
q.push(i);
}
}
int64 ret=0;
while(!q.empty()) {
const int x=q.front();
q.pop();
for(auto &y:e[x]) {
dis[y]=std::max(dis[y],dis[x]+f[y]);
if(!--ind[y]) {
q.push(y);
}
}
ret=std::max(ret,dis[x]);
}
return ret;
}
public:
int64 getMinimum(const int &n,const std::vector<std::string> &s,const std::vector<std::string> &t) {
init(n);
const int m=s.size();
for(register int i=0;i<m;i++) {
int w=0,x=0,y=0,z=0,o=0,p=0,q=0,r=0;
const int l=s[i].length();
for(register int j=0;j<l;j++) {
if(s[i][j]=='A') w++;
if(s[i][j]=='B') x++;
if(s[i][j]=='C') y++;
if(s[i][j]=='D') z++;
if(t[i][j]=='A') o++;
if(t[i][j]=='B') p++;
if(t[i][j]=='C') q++;
if(t[i][j]=='D') r++;
}
for(register int i=1;i<=tot;i++) {
if(a[i]<w||b[i]<x||c[i]<y||d[i]<z) continue;
const int &j=id[a[i]-w+o][b[i]-x+p][c[i]-y+q];
if(i==j) continue;
add_edge(i,j);
edge.emplace_back((Edge){i,j});
}
}
for(register int i=1;i<=tot;i++) {
if(!dfn[i]) tarjan(i);
}
for(register int i=1;i<=tot;i++) {
e[i].clear();
}
for(auto &e:edge) {
const int &u=e.u,&v=e.v;
if(scc[u]!=scc[v]) {
add_edge(scc[u],scc[v]);
ind[scc[v]]++;
}
}
return kahn();
}
};
[TC11326]ImpossibleGame的更多相关文章
- NOI.AC NOIP模拟赛 第二场 补记
NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...
随机推荐
- crontab定时任务2_net
2017年2月25日, 星期六 crontab定时任务2_net 1.先来一个小小的例子 查看当前路径: [root@root test]# pwd /home/admin/test [root@ro ...
- [iOS]深拷贝/浅拷贝区别
来点鸡汤: // 所谓拷贝 就是在原有的对象的基础上产生一个新的副本对象.有两点原则: // 1. 改变原对象的属性和行为不会影响副本对象 // 2. 改变副本对象的属性和行为不会影响原对象 ...
- 本日吐槽!“人傻钱多”的P2P公司是否是程序员的合适选择(群聊天记录的娱乐)
这个题目“P2P的职位是否是程序员的合适选择”这个问题本身是没啥可以吐槽的 但是每当我们讨论那种类型的公司工资愿意给前端工程师开的最高的时候,P2P这个行业被第一个提出了 目前我收到过面试的企业类型千 ...
- html5 canvas简单的直线路径
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 20155339 2016-2017-2 《Java程序设计》第5周学习总结
20155339 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 使用try.catch 使用try.catch语法,JVM会先尝试执行try区块中的代码,如 ...
- MFS - MooseFS 文件系统
MFSMooseFS 文件系统 可以实现RAID 功能:节约成本 实现在线扩展:是一种半分布式文件系统. 一.MFS文件系统的组成 1.mfsmaster 元数据服务器. 在整个体系中负责管理管理文件 ...
- MongoDB 之 手把手教你增删改查 MongoDB - 2
我们在 MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1 中学习了如果安装部署一个 MongoDB 如果没看到我的金玉良言的话,就重新打开一次客户端和服务端吧 本章我们 ...
- 如何使用gifsicle压缩gif图片
最近我写了一些关于如何将各种形式的多媒体格式相互转换的文章,特别是GIF动图方面的,比如如何将小视频转换成GIF动图或将GIF动图转换成视频,有很多像ImageMagick,ffmpeg这样的工具帮助 ...
- su: cannot set user id: Resource temporarily unavailable【转】
今天R&D所在主机出现su: cannot set user id: Resource temporarily unavailable资源不可用报错,直接通过其他机器ssh huyuh@xxx ...
- MySQL性能剖析工具(pt-query-digest)【转】
这个工具同样来自percona-toolkit 该工具集合的其他工具 MySQL Slave异常关机的处理 (pt-slave-restart) 验证MySQL主从一致性(pt-table-chec ...