跳到正文

个人记录

CF_467_D Fedor and Essay (DAG_dp+缩点)

hgzxgzx约 9 分钟
原文
文章信息与版本

文章信息

发布
2022年9月27日
收录
2026年7月9日
更新
2026年11月18日
篇幅
约 9 分钟

版本

  1. v1当前

CF_467_D

题意

给出 nn 个字符串,再给出 mm 对字符串的单向转换方式(即:如果字符串 S1S_1 可以转换为字符串 S2S_2,该转换只代表可以将前者转换为后者),现要求求出将这 nn 个字符串完成转换后的字符串中 R 最少出现次数,如果有多个满足的方法,则使得转换后的字符串的长度最小。

思路

  • 先将全部的字符串映射到整数范围,可以用 map 实现,但是注意这些字符串不区分大小写,也就是说我们还要统一标准,例如全部转化为小写。对于每一个字符串我们实际上只用维护两个值,即为 R 的出现次数与该串的长度。

  • 建图是很显然的,所以根据转换关系来建图,注意此时的图还是一张有向有环图,Tarjan 缩点就好。此时我们就觉察到 dp 的味道,所以我们不妨设定 dp 状态如下。

    dp(i):dp(i): 在缩点完成的图中,我们从点 ii(对应着一个字符串) 所能到达的最小的字符串(按照键值比较,先比 R 的次数,再比长度)。

  • DAG 上的 dp 就好,一个想法就是缩点过后建反图。

缩点生疏了,思路很简单,主要是一边划水一边打,效率是真的低。

Upd 2022/9/28 把字符从大写转化为小写运用到了向量的思想。

Code

C++
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<ctime>
#include<set>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#define File(x) freopen(""#x".in","r",stdin)
#define Look(x) freopen(""#x"","w",stdout)
#define rep(i,l,r) for(int i=l; i<r; ++i)
#define drep(i,r,l) for(int i=r; i>l; --i)
#define vi vector<int>
#define db double
#define debug cerr<<"ok"<<endl
#define CT cout<<TNT<<':'<<"--------------------------------"<<endl
using namespace std;
typedef long long LL;
int TNT;
const int N=3e5+10;
const int W=0x7f7f7f7f;
int times,low[N],dfn[N],scc[N],sta[N],top,tot,ot,ind[N],sum;
bool mark[N],inq[N];
struct graph{
	struct edge{
		int nxt,to;
	}e[N/3];	int head[N],cnt;
	inline void add(int f,int t){
		e[++cnt]=(edge){head[f],t};head[f]=cnt;
	}	
};
graph g1,g2;
struct Point{
	int sr;
	LL len;
	Point(){
		sr=len=0;
	}
	bool operator <(const Point &a)const {
		if(sr!=a.sr) return sr<a.sr;
		return len<a.len;
	}
};
struct Side{
	int u,v;
}side[N];
Point dp[N],ori[N],late[N];
string str[N/3];
map<string,int> mp;
map<pair<int,int>,bool> rec;
template <class T>
void Rd(T &a){
	a=0;int f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {a=(a<<1)+(a<<3)+ch-'0';ch=getchar();}
	a*=f;
}
void trans(string &a){
	int len=a.size();
	rep(i,0,len){
		if(!(a[i]>='a'&&a[i]<='z')) a[i]+='a'-'A';
	}
}
Point calc(string a){
	Point res;
	for(auto x: a) if(x=='r') res.sr++;
	res.len=a.size();
	return res;
}
void Tarjan(int u){
	dfn[u]=low[u]=++times;
	sta[++top]=u;
	mark[u]=true,inq[u]=true;
	for(int i=g1.head[u]; i; i=g1.e[i].nxt){
		int v=g1.e[i].to;
		if(!dfn[v]){
			Tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(inq[v]) low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u]==low[u]){
		sum++;
		do{
			#ifdef DEBUG
			CT;
			if(sta[top]==7){
				cout<<ori[sta[top]].sr<<' '<<ori[sta[top]].len<<endl;
			}
			CT;
			TNT++;
			#endif
			scc[sta[top]]=sum;
			inq[sta[top]]=false;
			if(ori[sta[top]]<dp[sum]) dp[sum]=ori[sta[top]];
		}while(sta[top--]!=u);
	}
}
int main(){
	#ifndef ONLINE_JUDGE
	File(ts);
	#endif
	int n,m;
	Rd(n);
	rep(i,1,n+1){
		cin>>str[i];
		trans(str[i]);
		if(mp.find(str[i])==mp.end()) mp[str[i]]=++tot,ori[tot]=calc(str[i]);
	}
	Rd(m);
	rep(i,1,m+1){
		string str1,str2;
		cin>>str1>>str2;
		trans(str1),trans(str2);
		if(mp.find(str1)==mp.end()) mp[str1]=++ot+tot,ori[ot+tot]=calc(str1);// ot -> "other"
		if(mp.find(str2)==mp.end()) mp[str2]=++ot+tot,ori[ot+tot]=calc(str2);
		int u=mp[str1],v=mp[str2];
		#ifdef DEBUG
		CT;
		cout<<str1<<' '<<str2<<endl;
		cout<<"first:"<<' '<<ori[u].sr<<' '<<ori[u].len<<endl;
		cout<<"second:"<<' '<<ori[v].sr<<' '<<ori[v].len<<endl;
		CT;
		TNT++;
		#endif
		g1.add(u,v);// digraph with loops
		side[i]=(Side){u,v};
	}
	rep(i,1,ot+tot+1) dp[i].sr=W,dp[i].len=W;// initalization
	rep(i,1,ot+tot+1) if(!mark[i]) Tarjan(i),top=0;
	rep(i,1,m+1){
		if(scc[side[i].u]!=scc[side[i].v]&&rec.find(make_pair(scc[side[i].v],scc[side[i].u]))==rec.end()){
			g2.add(scc[side[i].v],scc[side[i].u]);
			rec[make_pair(scc[side[i].v],scc[side[i].u])]=true;
			ind[scc[side[i].u]]++;
		}
	}
	top=0;
	rep(i,1,sum+1) if(!ind[i]) sta[++top]=i;
	while(top){
		int u=sta[top--];
		for(int i=g2.head[u]; i; i=g2.e[i].nxt){
			int v=g2.e[i].to;
			if(dp[u]<dp[v]) dp[v]=dp[u];
			ind[v]--;
			if(!ind[v]) sta[++top]=v;
		}
	}
	int sr=0;
	LL tlen=0;
	#ifdef DEBUG
	CT;
	cout<<mp["rks"]<<endl;
	cout<<scc[mp["raks"]]<<' '<<scc[mp["rks"]]<<endl;
	cout<<dp[scc[mp["raks"]]].sr<<' '<<dp[scc[mp["raks"]]].len<<endl;
	cout<<dp[scc[mp["rks"]]].sr<<' '<<dp[scc[mp["rks"]]].len<<endl;
	CT;
	TNT++;
	#endif
	rep(i,1,n+1){
		sr+=dp[scc[mp[str[i]]]].sr,tlen+=dp[scc[mp[str[i]]]].len;
		#ifdef DEBUG
		CT;
		cout<<str[i]<<endl;
		cout<<dp[scc[mp[str[i]]]].sr<<' '<<dp[scc[mp[str[i]]]].len<<endl;
		CT;
		TNT++;
		#endif
	}
	printf("%d %lld",sr,tlen);
	return 0;
}
/*
2022/9/27
start thinking at 18:40
start coding at 18:52
finish debugging at 21:53
*/
原文