JZOJ 3207.Orthogonal Anagram
\(\text{Problem}\)
给出一个字符串,求经过重新排列的另一个字典序最小的字符串,满足:相同的位置上
原串与结果串的字符不同。不存在则输出空串。
\(\text{Solution}\)
考虑从第一位开始枚举匹配
如果这位匹配(即两个都不相同)某个字符后可以判断出剩下的字符能否合法匹配
那这位就匹配这个字符即可
如何判断?
考虑剩下需要匹配的字符个数和还没用的字符个数
那么就可以建出一个网络流模型
每个字符拆成两个点
源点向 \(26\) 个字符连边,边权为还可用的个数
\(26\) 个字符向汇点连边,边权为还需要的个数
字符间连能匹配的点
判断是否满流即可
可以获得 \(90pts\)
\(\text{Code}\)
#include <cstdio>
#include <cstring>
#include <iostream>
#define re register
using namespace std;
const int N = 5e4 + 5, M = 70, S = 54, T = 55;
char s[N];
int n, tot, h[M], cur[M], dep[M], Q[M], s1[N], s2[N];
struct edge{int to, nxt, w;}e[M * M];
inline void add(int x, int y, int z){e[++tot] = edge{y, h[x], z}, h[x] = tot;}
inline int bfs()
{
for(re int i = 0; i <= T; i++) cur[i] = h[i], dep[i] = 0;
int head = 0, tail = 1; Q[1] = S, dep[S] = 1;
while (head < tail)
{
int now = Q[++head];
for(re int i = h[now]; i; i = e[i].nxt)
{
int v = e[i].to;
if (dep[v] || !e[i].w) continue;
dep[v] = dep[now] + 1, Q[++tail] = v;
}
}
return dep[T];
}
int dfs(int x, int lim)
{
if (x == T || lim <= 0) return lim;
int flow = 0;
for(re int i = cur[x]; i; i = e[i].nxt)
{
cur[x] = i;
int v = e[i].to;
if (dep[v] != dep[x] + 1 || !e[i].w) continue;
int f = dfs(v, min(lim, e[i].w));
if (f <= 0) continue;
e[i].w -= f, e[i ^ 1].w += f, lim -= f, flow += f;
if (lim <= 0) break;
}
return flow;
}
inline int dinic()
{
int res = 0;
while (bfs()) res += dfs(S, N);
return res;
}
inline int check(int y, int x)
{
if (!s1[y]) return 0;
tot = 1, memset(h, 0, sizeof h);
int total = 0;
--s1[y], --s2[x];
for(re int i = 0; i < 26; i++)
{
if (s1[i]) add(S, i, s1[i]), add(i, S, 0);
if (s2[i]) add(i + 26, T, s2[i]), add(T, i + 26, 0);
total += s2[i];
}
for(re int i = 0; i < 26; i++)
for(re int j = 0; j < 26; j++)
if (i ^ j) add(i, j + 26, N), add(j + 26, i, 0);
int flow = dinic();
if (flow >= total) return 1;
++s1[y], ++s2[x];
return 0;
}
int main()
{
scanf("%s", s + 1), n = strlen(s + 1);
for(re int i = 1; i <= n; i++) ++s1[s[i] - 'a'], ++s2[s[i] - 'a'];
for(re int i = 1; i <= n; i++)
{
int fl = 0;
for(re int j = 0; j < 26; j++)
if (j != s[i] - 'a' && check(j, s[i] - 'a'))
{
printf("%c", j + 'a'), fl = 1;
break;
}
if (!fl) break;
}
}
正解则是加过判断速度
一个结论:完全匹配的充要条件是 \(\forall i \in \sum,f_i+g_i<=length\)
\(f,g\) 是这个字符可用个数和需要匹配个数,\(length\) 为总字符数
\(\text{Code}\)
#include <cstdio>
#include <cstring>
#include <iostream>
#define re register
using namespace std;
const int N = 5e4 + 5;
char s[N];
int n, s1[50], s2[50];
inline int check(int y, int x, int len)
{
if (!s1[y]) return 0;
--s1[y], --s2[x];
int fl = 1;
for(re int i = 0; i < 26; i++)
if (s1[i] + s2[i] > len){fl = 0; break;}
if (fl) return 1;
++s1[y], ++s2[x];
return 0;
}
int main()
{
scanf("%s", s + 1), n = strlen(s + 1);
for(re int i = 1; i <= n; i++) ++s1[s[i] - 'a'], ++s2[s[i] - 'a'];
for(re int i = 1; i <= n; i++)
{
int fl = 0;
for(re int j = 0; j < 26; j++)
if (j != s[i] - 'a' && check(j, s[i] - 'a', n - i))
{
printf("%c", j + 'a'), fl = 1;
break;
}
if (!fl) break;
}
}
JZOJ 3207.Orthogonal Anagram的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- Leetcode Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- LeetCode 242 Valid Anagram
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- 【POJ】3207 Ikki's Story IV - Panda's Trick
http://poj.org/problem?id=3207 题意:一个圆上顺时针依次排列着标号为1-n的点,这些点之间共有m条边相连,每两个点只能在圆内或者圆外连边.问是否存在这些边不相交的方案.( ...
- 【09_242】Valid Anagram
Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...
- 【leetcode❤python】242. Valid Anagram
class Solution(object): def isAnagram(self, s, t): if sorted(list(s.lower()))==sorted(list ...
- 242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- (easy)LeetCode 242.Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- 【BZOJ】【3207】花神的嘲讽计划 I
字符串Hash+可持久化线段树 好神奇的转化…… 蒟蒻一开始还去想AC自动机去了……然而由于a[i]的范围是小于等于n,怎么也想不出一个时间复杂度合理的方法 膜拜了题解0.0原来是字符串Hash! 首 ...
随机推荐
- Day24.1:抽象类的详解
抽象类 1.1抽象类概述 一个动物类中,我们创建对象时会去new一个动物:但是我们不应该直接创建动物这个对象,因为动物本身就是抽象的,没有动物这种实例,我们创建的应该是一个具体的动物类,比如猫.狗等动 ...
- nginx配置文件讲解及示例(可复制)
详细的配置说明参考:https://www.cnblogs.com/ghl1024/p/9013805.html [示例一] #运行用户user www-data; #启动进程,通常设置成和cpu ...
- 自定义RBAC(4)
您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来- 前面把RBAC的权限系统设计过程都讲清楚了,现在就来实现它.大致分这么几个步骤: 1.先定义出完整的权限系统表结构: 2.实现Entity.Da ...
- 0停机迁移Nacos?Java字节码技术来帮忙
摘要:本文介绍如何将Spring Cloud应用从开源Consul无缝迁移至华为云Nacos. 本文分享自华为云社区<0停机迁移Nacos?Java字节码技术来帮忙>,作者:华为云PaaS ...
- Linux 下使用Docker 安装 LNMP环境 超详细
首先在阿里云购买了一台服务器 选择了华南-深圳地区 操作系统选用了 CentOS8.0 64位 1. 初始化账号密码 登陆xshell,开始装Docker 一.安装docker 1.Docker 要求 ...
- ArcObjects SDK开发 021 开发框架搭建-FrameWork包设计
1.框架引擎部分 引擎模块其实就是之前我们说的App-Command-Tool模块,通过这个模块,把系统的主干框架搭建起来. 其中大部分出现在菜单以及工具条上的按钮都会继承这个框架定义ICommand ...
- 数位排序【第十三届蓝桥杯省赛C++C组】
数位排序 小蓝对一个数的数位之和很感兴趣,今天他要按照数位之和给数排序. 当两个数各个数位之和不同时,将数位和较小的排在前面,当数位之和相等时,将数值小的排在前面. 例如,\(2022\) 排在 \( ...
- 多目标优化经典算法——NSGA-II
参考博客链接 https://blog.csdn.net/qq_35414569/article/details/79639848?utm_medium=distribute.pc_relevant. ...
- windows安装wordcloud遇到的坑汇总
pip install wordcloud报错,缺少visual studio包 不要偷懒,一定要从报错的地方去下载完整版本 然后安装c++ 重启后就不会报错了
- 解决前端发送post 请求出现403,cancled等问题
问题一:页面初始加载,部分接口首次请求options是200,然后第二次post请求cancled状态 1. 检查console控制台报错,如果是接口问题,就不用操心了 2.如果是其他报错,那么就不用 ...