[FJWC 20190212] T3 不同的缩写

本文介绍了一种用于分配游戏内角色简称的算法,通过构建二分图并运用匈牙利算法进行匹配,确保角色简称的独特性和最短长度,适用于简化代码编写流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

并没有传送门

题目描述

你在写一款Galgame 的剧情(的代码)。

在这个游戏中一共有 n n n 个角色。你需要编写一些关于这些角色的对话内容。然而,在写这些对话内容之前,都要写一段关于角色信息的代码,就像这样:

Character("Alex", color = "#FFFC3A")

你觉得这样好麻烦。你决定把它简化一下。你打算用角色名字的一个子序列(可以不连续)来作为它的简称。

当然,不同的角色要用不同的字符串作为简称,否则你就变量重名了。

你想确定一个简称的分配方案使得所有角色中最长的简称尽量短,这样你打起代码就会方便一些。

输入输出格式

输入格式

从文件diff.in 中读入数据。

第一行一个正整数 n n n

接下来n 行,每行一个由小写字母组成的字符串,代表一个角色的名字。

不同的角色可能会有相同的名字。

输出格式

输出到文件diff.out 中。

如果不存在一种分配简称的方案满足条件,输出 1 1 1

否则第一行输出一个正整数,表示最长的简称的最小长度。

接下来 n n n 行每行一个字符串,按顺序表示每个角色的简称。

若有多种方案满足条件,那么你可以输出任意一种。

输入输出样例

输入样例#1:
11
night
nealchen
beimingyouyu
echo
rankinf
dntcrybecthlev
lagoon
cyc
alphagem
leehwincing
clin
输出样例#1:
1
g
a
m
e
r
b
o
y
l
w
c

解题分析

考试的时候接近想到正解了…

考虑 n ≤ 4 n\le 4 n4的情况, 直接大力枚举子序列, 然后二分图匹配。

然后会发现当字符串随机的时候大概率答案为 2 2 2 − 1 -1 1, 于是 60 p t s 60pts 60pts到手。

正解其实也差不远了, 根据 H a l l Hall Hall定理, 我们给每个名字最多找 n n n个子序列搞二分图匹配就好了。

再加个二分, 总复杂度 O ( n 3 l o g ( n ) ) O(n^3log(n)) O(n3log(n))

因为左边点的数量为 n n n个, 右边点的数量为 n 2 n^2 n2个, 边有 n 2 n^2 n2条, 所以匈牙利和网络流复杂度差不多。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <string>
#include <map>
#include <algorithm>
#define R register
#define IN inline
#define W while
#define MX 305
#define SIZ 200500
#define BASE 233
using std::cin;
using std::cerr;
using std::cout;
using std::endl;
int n, arr, cnt;
std::map <int, std::string> mp;
std::map <int, int> tab;
std::string Out[MX];
char str[MX][MX], res[MX];
int head[MX], mch[SIZ], to[MX][MX][26], len[MX], fir[MX][26];
int tot[MX];
bool vis[SIZ];
struct Edge {int to, nex;} edge[SIZ << 1];
IN void add(R int from, R int to) {edge[++cnt] = {to, head[from]}, head[from] = cnt;}
IN void pre(R int id)
{
	fir[id][str[id][len[id]] - 'a'] = len[id];
	for (R int i = len[id] - 1; i; --i)
	{
		int nex = str[id][i + 1] - 'a';
		fir[id][str[id][i] - 'a'] = i;
		for (R int j = 0; j < 26; ++j)
		{
			if (nex == j) to[id][i][j] = i + 1;
			else to[id][i][j] = to[id][i + 1][j];
		}
	}
}
void DFS(R int now, R int hs, R int id, R int dgt, R int lim)
{
	if (!tab[hs]) tab[hs] = ++arr, mp[arr] = res;
	add(id, tab[hs]); ++tot[id];
	if (tot[id] == n || dgt > lim) return res[dgt - 2] = '\0', void();
	for (int i = 0; i < 26; ++i)
	{
		if (to[id][now][i])
		{
			res[dgt - 1] = 'a' + i;
			DFS(to[id][now][i], hs * BASE + i + 1, id, dgt + 1, lim);
			if (tot[id] == n) return res[dgt - 2] = '\0', void();
		}
	}
	res[dgt - 2] = '\0';
}
bool Hungary(R int now)
{
	for (R int i = head[now]; i; i = edge[i].nex)
	{
		if (!vis[edge[i].to])
		{
			vis[edge[i].to] = true;
			if ((!mch[edge[i].to]) || Hungary(mch[edge[i].to]))
			{
				mch[edge[i].to] = now;
				return true;
			}
		}
	}
	return false;
}
int main(void)
{
	freopen("diff.in", "r", stdin);
	freopen("diff.out", "w", stdout);
	std::ios_base::sync_with_stdio(false);
	cin >> n;
	for (R int i = 1; i <= n; ++i)
	{
		cin >> (str[i] + 1);
		len[i] = std::strlen(str[i] + 1);
		pre(i);
	}
	int lef = 1, rig = n, ans = -1, mid;
	W (lef <= rig)
	{
		mid = lef + rig >> 1; //cerr << mid << endl;
		std::memset(head, cnt = 0, sizeof(head));
		std::memset(mch, 0, sizeof(mch));
		std::memset(tot, 0, sizeof(tot));
		for (R int i = 1; i <= n; ++i)
		{
			for (R int j = 0; j < 26; ++j)
			{
				if ((!fir[i][j]) || tot[i] == n) continue;
				res[0] = 'a' + j;
				DFS(fir[i][j], j + 1, i, 2, mid);
			}
		}
		int s = 0;
		for (R int i = 1; i <= n; ++i)
		{
			std::memset(vis, false, sizeof(vis));
			s += Hungary(i);
		}
		if (s == n)
		{
			ans = mid;
			rig = mid - 1;
			for (R int i = 1; i <= arr; ++i)
			if (mch[i]) Out[mch[i]] = mp[i];
		}
		else lef = mid + 1;
	}
	cout << ans << endl;
	if (ans > 0)
	for (R int i = 1; i <= n; ++i) cout << Out[i] << endl;
}

注意别作死清空装了string的map。

PS:

用Linux的同学注意了, 这道题叫 d i f f diff diff, 对拍的时候…

出题人真TM毒, 双系统卡对拍…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值