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
  • 相关阅读:
    面经 收藏的 这可能不只是一篇面经
    2017网易秋招编程集合
    网易2017春招笔试真题编程题集合题解
    小朋友 排序
    网易编程题目 02 赶去公司
    网易有道2017内推选择题
    12,享元模式(Flyweight Pattern)是以共享的方式高效的支持大量的细粒度的对象。
    11,外观模式(Facade Pattern)是为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
    新手进行Oracle MIS系统开发的步骤
    Oracle常用查看表结构命令(一)
  • 原文地址:https://www.cnblogs.com/bolderic/p/7155910.html
Copyright © 2011-2022 走看看