zoukankan      html  css  js  c++  java
  • CFRound423_Div2 C String Reconstruction

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

    或许对于我来说的玄学做法你们并不在意。

    题目大意很清楚,就是构建一个字典序最小的字符串满足给出的几个要求。直接暴力肯定不行,想优化怎么也想不出来,偶然看到一AC的代码感觉非常厉害,按照思路自己写了一遍。

    大体思路:每次将子串加入结果串的时候利用并查集的思想(个人见解)将整个子串作为一个整体,而其父亲即是这个整体紧邻的那个点。

    也就是说每次遇到已经赋值的点时直接跳到它的下一个空点处,即它父亲,这样就不会超时了。。。。

    代码:

     1 #define maxn 3000100
     2 char ans[maxn], tmp[maxn];
     3 int f[maxn];
     4 int n, len, k, c, maxcur = 0;
     5 
     6 int get(int x) { return x == f[x]? x: f[x] = get(f[x]); }
     7 void unite(int x, int y){
     8     int rx = get(x), ry = get(y);
     9     if(rx == ry) return;
    10     else f[rx] =ry;
    11 }
    12 
    13 int main(){
    14     memset(ans, 0, sizeof(ans));
    15     for(int i = 0; i < maxn; i++) ans[f[i] = i] = 'a';
    16     scanf("%d", &n);
    17     while(n--){
    18         scanf("%s", tmp + 1);
    19         len = strlen(tmp + 1);
    20         scanf("%d", &k);
    21         while(k--){
    22             scanf("%d", &c);
    23             maxcur = max(maxcur, c + len - 1);
    24             
    25             for(int i = c; i < c + len; i = get(i)){
    26                 ans[i] = tmp[i - c + 1];
    27                 unite(i, i + 1);
    28             }
    29         }
    30     }
    31     ans[maxcur + 1] = '';
    32     puts(ans + 1);
    33 } 

    题目:

    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 sconsist 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
    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
  • 相关阅读:
    用一个案列详细讲解UITextFiled
    iOS开发中的内存分配(堆和栈)
    一次性解决导航栏的所有问题
    iOS 枚举的巧用
    iOS高仿app源码:纯代码打造高仿优质《内涵段子》
    如何实现百度外卖APP个人中心头像"浪"起来的动画效果
    手把手教你修改iOS版QQ的运动步数
    UIView的layoutSubviews和drawRect方法何时调用
    内存恶鬼drawRect
    iOS 离屏渲染的研究
  • 原文地址:https://www.cnblogs.com/bolderic/p/7155910.html
Copyright © 2011-2022 走看看