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 ...
随机推荐
- MyBatis官方文档——入门
入门 安装 要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于 classpath 中即可. 如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 ...
- Linux性能监控——CPU,Memory,IO,Network
版权声明:本文由刘爽原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/107 来源:腾云阁 https://www.qclou ...
- 【黑金ZYNQ7000系列原创视频教程】07.自定义IP——定制RTC IP实验
黑金论坛地址: http://www.heijin.org/forum.php?mod=viewthread&tid=36641&extra=page%3D1 爱奇艺地址: http: ...
- Oracle存储过程--案例
限额控制 CREATE OR REPLACE PACKAGE BODY NP_PCKG_MERCHANT_LIMIT AS PROCEDURE CHECK_LIMIT ( in_iplCode IN ...
- 强连通分量+缩点(poj2553)
http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total ...
- sprintf函数详解
转摘声明:选自<CSDN 社区电子杂志——C/C++杂志> 在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于sprintf 跟printf 在用法上几乎一样 ...
- AttributeError: 'module' object has no attribute 'face'
报错 raceback (most recent call last): File "D:/work/python/face_ai/predict.py", line 41, in ...
- JAVA参数没有引用传递,只有值传递
原文章地址:http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性, ...
- 模仿Masonary写一个计算器
1.CaculatorMaker @interface CaculatorMaker : NSObject @property(nonatomic,assign)int result; -(Cacul ...
- Kd-tree的学习
一.普通kd-tree 1.在选择划分维度的时候,不能简单的每一个维度轮流划分.还有一种更合适的是利用数据的方差来划分,哪个维度的方差大,就选择哪一个维度划分.理由解释如下: 最简单的方法就是轮着来, ...