String Reconstruction

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 tioccurs 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 tidoesn'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
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab

寻找一个字符串。给你一些线索(某些子串的位置),输出一个满足条件的最小字典序字符串。
暴力求解超时。可以采用并查集,若当前位置已经有子串放置,那就从已知子串的末尾继续放。并查集记录下末尾的下一位。很好的避免了重叠部分的重复枚举。有点像cys学长教过的那道next跳的题。
并查集套路题。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<iterator>
#define MAX 2000005
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std; typedef long long ll; int f[MAX];
char a[MAX];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
int main()
{
int n,m,x,len,i,j,k,ii;
char s[MAX];
scanf("%d",&n);
for(i=;i<=;i++){
a[i]='a';
f[i]=i;
}
int maxx=;
while(n--){
scanf(" %s %d",s,&k);
len=strlen(s);
for(j=;j<=k;j++){
scanf("%d",&x);
int xx=find(x);
int ff=find(len+x);
for(i=xx,ii=xx-x;i<=len+x-;i++,ii++){
a[i]=s[ii];
f[find(i)]=ff;
}
if(len+x->maxx) maxx=len+x-;
}
}
for(i=;i<=maxx;i++){
printf("%c",a[i]);
}
printf("\n");
return ;
}
 

CodeForces - 828C String Reconstruction 并查集(next跳)的更多相关文章

  1. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  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. JAVA数据类型(转)

     java中数据的基本类型分为: 基本数据类型和引用数据类型,对此不多介绍:   接下来讨论一下java中数据类型存储在哪     基本数据类型存储在哪,取决于基本类型在哪声明:          1 ...

  2. 借助nodejs解析加密字符串 node安装库较python方便

    const node_modules_path = '../node_modules/' // crypto-js - npm https://www.npmjs.com/package/crypto ...

  3. Kubernetes TensorFlow 默认 特定 集群管理器

    Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...

  4. Java 中的四种引用类型(转)

    目录 背景 简介          1. 强引用 StrongReference          2. 弱引用 WeakReference          3. 软引用 SoftReference ...

  5. 【题解】[SCOI2010]股票交易

    十分普通的DP+不平凡的转移 传送门 这道题状态十分明显.转移是\(O(n^4)\)的,过不去,我们需要优化. 一个十分显然的DP是\(f(i,j)\)表示第\(i\)天时候拥有\(j\)单位股票的最 ...

  6. JavaScript 四种显示数据方式

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 升级pip3的正确姿势

    如果你的电脑里装了两个python,就会有两个pip,一个是pip2,一个是pip3,还有可能出现一个既没有2也没有3的pip,一般情况下,pip等于pip2 有时候我们使用pip安装东西会提示我们p ...

  8. ubuntu 搭建 tomcat

    一.下载tomcat 先下载到本地,然后ftp上传到服务器 官方 Apache Tomcat 的下载 2 二.解压安装 先解压 tar zxvf apache-tomcat-7.0.64.tar.gz ...

  9. redis下载及安装服务

    1 . 要安装Redis,首先要获取安装包. Windows的Redis安装包需要到以下GitHub链接找到. 链接:https://github.com/MSOpenTech/redis 打开网站后 ...

  10. 4.2 《锋利的jQuery》jQuery中的动画

    问题:queue()方法? tip0: jquery从1.9版本以上就不支持toggle()方法. // $("#panel h5.head").toggle(function() ...