Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C
2 seconds
256 megabytes
standard input
standard output
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
The first line contains single integer n (1 ≤ n ≤ 105) — the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order — positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 ≤ xi, j ≤ 106, 1 ≤ ki ≤ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Print lexicographically minimal string that fits all the information Ivan remembers.
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
abacaba
1
a 1 3
aaa
3
ab 1 1
aba 1 3
ab 2 3 5
ababab
题意:
求一个字典序最小的字符串,满足若干个字符串在此字符串中的出现情况。
采取逐个位置写入的方法。为了避免超时,写过的位置就不能重新写,而需找到后面第一个没有被写入的位置。那如果找到这个位置呢?如果直接往后遍历,那就跟重复写入没有区别了,所以,我们需要一个快速找到后面第一个没有被写入的位置的方法,可用并查集的find()函数。灵感来源:POJ1456 Supermarket 。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e6+; int fa[MAXN]; //fa[]用于记录此位置后面第一个没有被写入位置
bool used[MAXN];
int find(int x)
{
return !used[x]?x:fa[x]=find(fa[x]);
} char s[MAXN], tmp[MAXN];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(s, , sizeof(s));
memset(used, , sizeof(used));
for(int i = ; i<MAXN-; i++)
fa[i] = i+;
for(int i = ; i<=n; i++)
{
scanf("%s",tmp);
int sz = strlen(tmp);
int m, x;
scanf("%d",&m);
while(m--)
{
scanf("%d",&x);
for(int i = ; i<sz;)
{
if(!s[x+i]) //此位置没有被写入,则填写
{
s[x+i] = tmp[i];
used[x+i] = ;
i++;
}
else i = find(x+i)-x; //否则,找到后面位置第一个没有被写入的相对位置
}
}
} int sz = MAXN-;
while(!s[sz]) sz--;
for(int i = ; i<=sz; i++)
{
if(s[i]==) putchar('a'); //把没有写入的位置都填上‘a’
else putchar(s[i]);
}
putchar('\n');
}
}
Codeforces - 828C String Reconstruction —— 并查集find()函数的更多相关文章
- CodeForces - 828C String Reconstruction 并查集(next跳)
String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...
- CodeForces 828C String Reconstruction(并查集思想)
题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第 ...
- 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 Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
- Codeforces 650C Table Compression (并查集)
题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...
- Codeforces 468B Two Sets 并查集
题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
- CodeForces 566D Restructuring Company (并查集+链表)
题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...
随机推荐
- hibernate向mysql插入数据后,得到该条数据主键的方法
hibernate向MySQL插入一条数据后,得到该条数据主键的方法.主键是自增长的. 保存完成后,直接用该实体的getId的方法就可以得到.因为保存完成后,hibernate会自动将id赋值给实体. ...
- Web前端开发规范收集
在Web开发中,后端跟前端配合非常easy出现故障.这时我们就须要一些规则来约束前端任意的编写. CSS编程规范 1. 属性书写基本顺序 a. 先位置属性(position, to ...
- AAuto如何设置定时器
在设计视图中(一定要有个Form)点击左下角的功能组件 点击定时器即可切换到代码视图,并添加如下代码.其中我每隔一秒改变一下winform.static2.text的文本值
- java:可变参数(转载)
http://12477787.blog.51cto.com/12467787/1887843 Java在1.5之后允许方法使用可变参数,可变参数的好处在于:它允许传递0个或者多个参数.比如原来有一段 ...
- React15.6.0实现Modal弹层组件
代码地址如下:http://www.demodashi.com/demo/12315.html 注:本文Demo环境使用的是我平时开发用的配置:这里是地址. 本文适合对象 了解React. 使用过we ...
- mha安装报错 [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln361] None of slaves can be master. Check failover configuration file or log-bin settings in my.cnf
查找资料 参考 http://blog.51cto.com/16769017/1878451 解决方法: 在两个从库上开启二进制日志即可(花了 一天时间,找不到解决方法,最后还是靠自己的理解及测试解决 ...
- 1M网速等于多少K
http://zhidao.baidu.com/question/157400316.html&__bd_tkn__=65ac453b343794385019e962bfb06bb8c710d ...
- Oraclet提交提示Record is locked by another user错误
http://blog.csdn.net/alifel/article/details/4324338下午修改oracle datebase中的字段时,提示"Record is locked ...
- iphone手机分辨率--持久维护
6.5英寸 —— 1242 x 2688 px —— Xs Max 6.1英寸 —— 828 x 1792 px —— XR 5.8英寸 —— 1125 x 2436 px —— X/Xs 5.5英寸 ...
- sql数据库log自动增长被取消
原因分析:数据库可分配空间为0 解决方法:增加数据库初始大小