题目链接:http://codeforces.com/contest/828/problem/C

C. String Reconstruction
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print lexicographically minimal string that fits all the information Ivan remembers.

Examples
input

Copy
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
output

Copy
abacaba
input

Copy
1
a 1 3
output

Copy
aaa
input

Copy
3
ab 1 1
aba 1 3
ab 2 3 5
output

Copy
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()函数的更多相关文章

  1. CodeForces - 828C String Reconstruction 并查集(next跳)

    String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...

  2. CodeForces 828C String Reconstruction(并查集思想)

    题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第 ...

  3. Codeforces 828C String Reconstruction【并查集巧妙运用】

    LINK 题目大意 给你n个串和在原串中的出现位置,问原串 思路 直接跑肯定是GG 考虑怎么优化 因为保证有解,所以考虑过的点我们就不再考虑 用并查集维护当前每个点之后最早的没有被更新过的点 然后就做 ...

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

  5. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  6. Codeforces 650C Table Compression (并查集)

    题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...

  7. Codeforces 468B Two Sets 并查集

    题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...

  8. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  9. CodeForces 566D Restructuring Company (并查集+链表)

    题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...

随机推荐

  1. SQLServer-----SQLServer 2008 R2安装

  2. Unity3D开发基础组件提取总结

    在游戏开发过程中,除了逻辑功能的开发之外,还有非常多基础的模块.这些模块,对大部分手机网络游戏来说都是一样的.所以,在上个游戏已经上线运营大半年之际,我认为有必要将这些模块整理出来.让后面其它游戏的开 ...

  3. RR调度(Round-robin scheduling)简单介绍

    在RR调度策略下,一个线程会一直运行.直到: 自愿放弃控制权 被更高优先级的线程抢占 时间片用完 例如以下图所看到的,A在用完自己的时间片后,将CPU运行权让给线程B.于是A离开Read队列,而B进入 ...

  4. csdn开源夏令营-ospaf中期报告

    1.背景         随着将中期的代码托管到CSDN的平台上,ospaf(开源项目成熟度分析工具)已经有了小小的雏形.当然还远远不够.       首先还是要感谢这次活动组织方CSDN,感觉挺有G ...

  5. linq小实例

    var cus = from u in context.IPPhoneInfo join r in context.Organization on u.OrgStructure equals r.Mi ...

  6. Javascript模式(一) 单例模式

    function A(){ // 存储实例对象 var instance; // 重写构造函数,只返回闭包内的局部变量instance A = function(){ return instance; ...

  7. oracle 表压缩技术

    压缩表是我们维护管理中常常会用到的.以下我们看都oracle给我们提供了哪些压缩方式. 文章摘自"Oracle® Database Administrator's Guide11g Rele ...

  8. Hibernate学习六----------CRUD

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  9. android 自己定义组件随着手指自己主动画圆

    首先自己定义一个View子类: package com.example.androidtest0.myView; import android.content.Context; import andr ...

  10. Java提高篇

    http://www.cnblogs.com/chenssy/p/3850230.html http://www.cnblogs.com/chenssy/p/3521565.html http://w ...