Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集
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 stringsti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string sconsist 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 exceed106, 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
我同学都用排序 + 并查集,然而我用链表 + 并查集思想。
因为数据合法不用判错,所以用链表维护还未确定的位置。每条信息,找第一个确定的位置时边找边压缩路径(并查集思想)。然后按题意弄一下即可。
另外吐槽一下这道题。。直接交代码在第十个点各种WA,RE(必须交G++14才能AC,而且跑得非常慢),然后cf上交文件AC。。(可能是你在逗我)
Code
/**
* Codeforces
* Problem#828C
* Accepted
* Time:296ms
* Memory:59888k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const signed long long llf = (signed long long)((1ull << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} int n;
int m = ;
string* strs;
vector<int> *pos;
boolean* exist;
char* res;
int* suf;
int* pre; inline void init() {
readInteger(n);
strs = new string[n];
pos = new vector<int>[n];
for(int i = ; i < n; i++) {
static int c, x, l;
cin >> strs[i];
l = strs[i].length();
readInteger(c);
for(int j = ; j < c; j++) {
readInteger(x);
pos[i].push_back(x);
smax(m, x + l - );
}
}
} inline void remove(int x) {
pre[suf[x]] = pre[x];
suf[pre[x]] = suf[x];
exist[x] = false;
} int find(int x, int endpos) {
if(exist[x] || x >= endpos) return x;
return suf[x] = find(suf[x], endpos);
} inline void solve() {
exist = new boolean[m + ];
suf = new int[m + ];
pre = new int[m + ];
res = new char[m + ];
memset(exist, true, sizeof(boolean) * (m + ));
suf[n + ] = m + , pre[] = ;
for(int i = ; i <= m; i++) suf[i] = i + ;
for(int i = ; i <= m + ; i++) pre[i] = i - ; for(int i = ; i < n; i++) {
int l = strs[i].length();
for(int j = ; j < (signed)pos[i].size(); j++) {
int p = pos[i][j], start = pos[i][j];
p = find(p, start + l);
while(p < pos[i][j] + l) {
res[p] = strs[i][p - start];
remove(p);
p = suf[p];
}
}
} for(int i = ; i <= m; i++)
if(exist[i])
res[i] = 'a';
res[m + ] = ; puts(res + );
} int main() {
init();
solve();
return ;
}
Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集的更多相关文章
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B
Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It i ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力
题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)
Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A.String Reconstruction B. High Load C ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
随机推荐
- svn中给个地址,然后把自己建立的项目拖进去
1.首先checkout 那个地址就会得到一个空的文件夹(里面有.svn文件) 2.把你的项目copy一下,粘贴到你chekout的文件夹里面,所有文件都是?,然后选中全部,点击add,然后在comm ...
- Spring @Value注解 and Spring Boot @ConfigurationProperties注解
一.Spring的@Value Spring EL表达式语言,支持在XML和注解中表达式,类是于JSP的EL表达式语言. 在Spring开发中经常涉及调用各种资源的情况,包含普通文件.网址.配置文件. ...
- MySQL.Linux.安装
Linux 7.x.安装 MySQL 环境: linux是安装在虚拟机中的,宿主机是:win10系统.安装MySQL的时候,首先需要网络是通的(宿主机和虚拟机之间通信).相关配置,参见:虚拟机和宿主机 ...
- C# asp.net webapi下支持文件下载输出接口
/// <summary> /// 下载文件 /// </summary> public class DownloadController : ApiC ...
- clear/reset select2,重置select2,恢复默认
4.0 version //方法一$('#yourButton').on('click', function() { $('#yourfirstSelect2').val(null).trigger( ...
- centos安装java的jdk
1.下载 jdk-8u101-linux-x64.rpm http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- ...
- Python杨辉三角
杨辉三角,是二项式系数在三角形中的一种几何排列,在中国南宋数学家杨辉1261年所著的<详解九章算法>一书中出现.在欧洲,帕斯卡(1623----1662)在1654年发现这一规律,所以这个 ...
- 强化学习--Policy Gradient
Policy Gradient综述: Policy Gradient,通过学习当前环境,直接给出要输出的动作的概率值. Policy Gradient 不是单步更新,只能等玩完一个epoch,再 ...
- c++ 常用的数据结构
set // constructing sets #include <iostream> #include <set> void checkin(std::set<int ...
- html5-section元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...