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! 首 ...
随机推荐
- vivo大数据日志采集Agent设计实践
作者:vivo 互联网存储技术团队- Qiu Sidi 在企业大数据体系建设过程中,数据采集是其中的首要环节.然而,当前行业内的相关开源数据采集组件,并无法满足企业大规模数据采集的需求与有效的数据采集 ...
- oracle 内置函数(三)日期函数
日期函数概要: 系统时间 日期操作 一.系统时间 sysdate:还是西方的格式,我们一般需要to_char(date,'yyyy-mm-dd hh24:mi:ss') next_day:当前日期的下 ...
- 通过启动脚本控制PHP-FPM开关
vi /etc/init.d/php-fpm 复制粘贴以下内容: #! /bin/sh# Comments to support chkconfig on CentOS# chkconfig: 234 ...
- 【云原生 • Docker】mysql、tomcat、nginx、redis 环境部署
注意在应用部署之前,docker 服务必须是开启状态 systemctl start docker. 目录 一.MySQL 部署 二.Tomcat 部署 三.Nginx 部署 四.Redis 部署 一 ...
- 想早点下班?试试Aorm库吧,更方便的进行Go数据库操作
使用go进行项目开发,大多数人会使用gorm,但是gorm有一些缺点,我无法接受.于是开发出了aorm,目前能有满足日常开发需求,并且完善了使用文档,希望能够帮助到大家. Aorm Golang操作数 ...
- SQLMap入门——获取当前网站数据库的名称
列出当前网站使用的数据库 python sqlmap.py -u http://localhost/sqli-labs-master/Less-1/?id=1 --current-db
- 搭建漏洞环境及实战——搭建XSS测试平台
XSS测试平台是测试XSS漏洞获取cookie并接收Web页面的平台,XSS可以做成JS能做的所有事,包括但不限于窃取cookie.后台增删文章.钓鱼.利用CSS漏洞进行传播.修改网页代码.网站重定向 ...
- java中的字符串数组
本文主要讲述java中的字符串数组 字符串数组的声明有如下几种形式: // 第一种方式:new // 注意在String的后面[]中不需要添加字符串数组的长度.否则报错. String[] arr_1 ...
- 我的RHCA认证之旅
云方向的RHCA架构师认证 想更深入研究Linux.对Linux有一定兴趣,我在2022.12.27这一天通过了RHCA认证 课程介绍 以下是我在众多RHCA的专家课程中,选择的五门 cl210 (R ...
- 一文读懂 Kubernetes 存储设计
在 Docker 的设计中,容器内的文件是临时存放的,并且随着容器的删除,容器内部的数据也会一同被清空.不过,我们可以通过在 docker run 启动容器时,使用 --volume/-v 参数来指定 ...