CodeForces 828C String Reconstruction(并查集思想)
题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串。
思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高。
我们维护一个并查集fa,用fa[i]记录从第i位置开始第一个没填涂的位置,那每次都能跳过涂过的地方。每次填完当前格就去填find(fa[i + 1])。
ps:一定要合并,不然超时。
代码:
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = +;
const int seed = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
char ans[maxn*];
int fa[maxn*];
char s[maxn];
int find(int x){
return fa[x] == x? x : fa[x] = find(fa[x]);
}
int main(){
int n,t;
memset(ans,,sizeof(ans));
scanf("%d",&n);
for(int i = ;i < maxn * ;i++) fa[i] = i;
int end = ;
while(n--){
scanf("%s%d",s,&t);
int len = strlen(s);
while(t--){
int x;
scanf("%d",&x);
end = max(end,x + len - );
for(int i = find(x);i <= x + len -;i = find(i + )){
ans[i] = s[i - x];
int fx = find(i);
int fy = find(i + );
if(fx > fy)
fa[fy] = fx;
else
fa[fx] = fy;
}
}
}
for(int i = ;i <= end;i++)
if(ans[i] != )printf("%c",ans[i]);
else printf("a");
printf("\n");
return ;
}
CodeForces 828C String Reconstruction(并查集思想)的更多相关文章
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
- CodeForces - 828C String Reconstruction 并查集(next跳)
String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...
- Codeforces 828C String Reconstruction【并查集巧妙运用】
LINK 题目大意 给你n个串和在原串中的出现位置,问原串 思路 直接跑肯定是GG 考虑怎么优化 因为保证有解,所以考虑过的点我们就不再考虑 用并查集维护当前每个点之后最早的没有被更新过的点 然后就做 ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集
C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...
- Codeforces 650C Table Compression (并查集)
题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...
- PAT甲级1004题解——并查集思想改
题目分析:本题开始一直在考虑如何将每一个节点通过一种合适的数据结构存储起来(一对多的关系),最后发现借助并查集的思想可以用一个数组p,p[i]存放i节点的父节点,每次查询编号为i的节点属于第几层且判断 ...
- Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...
- UVA 12232 Exclusive-OR(并查集+思想)
题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现 ...
- Codeforces Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
随机推荐
- LeetCode——Peeking Iterator
Description: Given an Iterator class interface with methods: next() and hasNext(), design and implem ...
- DNS隧道通信的检测
DNS隧道通信的检测 DNS 隧道通信 DNS 隧道通信是C&C常用的通信方式,一般常用的编码方式Base64,Binary编码,NetBios编码等,Hex编码等.且请求的Type一般都是t ...
- Minix2.0操作系统公用头文件说明
以下头文件均在目录include/下: ansi.h: 用来检测编译器是否遵循标准C,如果是的话,_ANSI就被定义为31415,如果不是的,则_ANSI未定义.通过这个宏来诊测. limits.h: ...
- oracle rank over partition by
转自:https://www.cnblogs.com/wingsless/archive/2012/02/04/2338292.html rank() over(partition)的使用 有的时 ...
- 树形DP求各点能走到的最远距离
hdu2196 Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Composer 添加 Laravel-china 的国内源
不知道由于什么原因,原来的 Composer 的国内镜像 https://pkg.phpcomposer.com/ 不能正常使用,经常连不上. 找了半天,发现还有一个 laravel-china 的国 ...
- Python使用pyMysql模块插入数据到mysql的乱码解决
1.初步安装mysql,插入中文字符,出现的???的形式 终端提示: pymysql.err.InternalError: (1366, "Incorrect string value: ' ...
- 阿里云 elastic search 重启 过程
阿里云 es 重启 elasticsearch 重启 过程 实例变更中 53.13% 准备ECS资源 已完成节点数:4/4, 进度:100% 准备容器服务 进度:100% 变 ...
- NGINX:sticky模块实现基于cookie的负载均衡
Sticky模块 简述: 之前公司部署了一套网站及时发布系统,架构如下图所示:Nginx做前端代理,发布系统用tomcat运行,一台共享存储,一台数据库服务器:由于网站及时发布系统涉及到了用户登录操作 ...
- 【Linux学习 】Linux使用Script命令来记录并回放终端会话
一背景 二script命令简介 1 什么script命令 2 script命令操作 21 file选项 22 options选项 23 退出script 三Script命令结合实际使用场景 1 先在终 ...