\(\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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  3. 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 ...

  4. 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 ...

  5. 【POJ】3207 Ikki's Story IV - Panda's Trick

    http://poj.org/problem?id=3207 题意:一个圆上顺时针依次排列着标号为1-n的点,这些点之间共有m条边相连,每两个点只能在圆内或者圆外连边.问是否存在这些边不相交的方案.( ...

  6. 【09_242】Valid Anagram

    Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...

  7. 【leetcode❤python】242. Valid Anagram

    class Solution(object):    def isAnagram(self, s, t):        if sorted(list(s.lower()))==sorted(list ...

  8. 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 ...

  9. (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 ...

  10. 【BZOJ】【3207】花神的嘲讽计划 I

    字符串Hash+可持久化线段树 好神奇的转化…… 蒟蒻一开始还去想AC自动机去了……然而由于a[i]的范围是小于等于n,怎么也想不出一个时间复杂度合理的方法 膜拜了题解0.0原来是字符串Hash! 首 ...

随机推荐

  1. SpringDataJpa源码理解

    SpringDataJpa源码理解 上一篇讲解了SpringDataJpa的基本使用,下面简单说一下源码 我们以其中的一个test为案例进行分析: 我们可以发现resumeDao它是一个代理对象,类型 ...

  2. ValueError: Detected newline in header value. This is a potential security problem

    原因 flask框架进行重定向的url中包含 换行符\n或\r 解决方法 使用 strip() 函数去除行首或行尾的换行符(如果你url中间包含这些符号replace函数替换, 但是如果中间包含只能说 ...

  3. 【Flume】概述及组成、入门案例、进阶(事务、拓扑结构)、不同拓扑案例、自定义、数据流监控Ganglia

    一.概述 1.定义 日志采集.聚合.传输的系统,基于流式结构 即:读取本地磁盘数据,写入HDFS或kafka 2.架构 Agent:JVM进程,以事件形式将数据送到目的地. Agent由三部分组成:S ...

  4. 一篇文章教你实战Docker容器数据卷

    在上一篇中,咱们对Docker中的容器数据卷做了介绍.已经知道了容器数据卷是什么?能干什么用.那么本篇咱们就来实战容器数据卷,Docker容器数据卷案例主要做以下三个案例 1:宿主机(也就是Docke ...

  5. Flask框架使用SQLAlchemy的ORM

    SQLAlchemy 1.介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使用 ...

  6. Ajax---EventLoop事件循环

    前言       JavaScript 是一门单线程执行的脚本语言.也就是说,同一时间只能做一件事情. JavaScript要运行在宿主环境中(浏览器,nodejs)下.浏览器内部有执行js代码的引擎 ...

  7. JavaScript:操作符:正负号和自增自减及其隐式转换数据类型

    正负号 正号即加号,负号即减号,运算结果同数学意义一样: 对非数字类型进行正负号运算,会隐式转换为数字,再进行运算: 一些特殊的非数字,转换情况同算术运算符: 自增自减 自增即为++,自减即为--. ...

  8. JavaScript:类(class)

    在JS中,类是后来才出的概念,早期创造对象的方式是new Function()调用构造函数创建函数对象: 而现在,可以使用new className()构造方法来创建类对象了: 所以在很多方面,类的使 ...

  9. Python实验报告(第8章)

    实验8:模块 一.实验目的和要求 1.了解模块的内容: 2.掌握模块的创建和导入方式: 3.了解包结构的创建和使用. 二.实验环境 软件版本:Python 3.10 64_bit 三.实验过程 1.实 ...

  10. Excelize 2.7.0 发布, 2023 年首个更新

    Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准.可以使用它来读取.写入由 Microsoft Exc ...