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

LINK


题目大意

给你n个串和在原串中的出现位置,问原串


思路

直接跑肯定是GG
考虑怎么优化
因为保证有解,所以考虑过的点我们就不再考虑
用并查集维护当前每个点之后最早的没有被更新过的点
然后就做完了,很巧妙对吧


c++//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
  bool w = 1;x = 0;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-') w = 0, c = getchar();
  while (isdigit(c)) {
    x = (x<<1) + (x<<3) + c -'0';
    c = getchar();
  }
  if (!w) x = -x;
}
template <typename T>
void Write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x; 
  }
  if (x > 9) Write(x / 10);
  putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 1e6 + 10;
int n, fa[N << 1];
char s[N << 1], c[N];
void init() {
  fu(i, 1, (N << 1) - 1) fa[i] = i;
}
int find(int x) {
  return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main() {
  Read(n);
  init();
  int maxl = 0;
  fu(i, 1, n) {
    scanf("%s", c + 1);
    int num, len = strlen(c + 1), pos;
    Read(num);
    fu(j, 1, num) {
      Read(pos);
      maxl = max(maxl, pos + len - 1);
      for (int k = find(pos); k <= pos + len - 1; k = find(k)){
        s[k] = c[k - pos + 1];
        fa[k] = k + 1; 
      }
    }
  }
  fu(i, 1, maxl) if (!s[i]) s[i] = 'a';
  printf("%s", s + 1);
  return 0;
}

转载于:https://www.cnblogs.com/dream-maker-yk/p/9818587.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值