zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    LeetCode-216 Combination Sum III
    LeetCode-214 Shortest Palindrome
    LeetCode-212 Word Search II
    LeetCode-211 Add and Search Word
    LeetCode-210 Course Schedule II
    LeetCode-209 Minimum Size Subarray Sum
    LeetCode-208 Implement Trie (Prefix Tree)
    LeetCode-207 Course Schedule
    JavaEE--Mybatis学习笔记(四)--单表的CURD 补充
    JavaEE--Mybatis学习笔记(三)--单表的CURD
  • 原文地址:https://www.cnblogs.com/dream-maker-yk/p/9818587.html
Copyright © 2011-2022 走看看