Codeforces C - String Reconstruction

C - String Reconstruction

方法一:把确定的点的父亲节点设为下一个点,这样访问过的点的根节点都是没访问过的点。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
char s[N],res[N*2];
int fa[N*2];
int Find(int x)
{
    return x==fa[x]?x:fa[x]=Find(fa[x]);
}
int main()
{
    for(int i=0;i<N*2;i++)fa[i]=i;
    int n,t,x;
    scanf("%d",&n);
    int ml=0;
    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        scanf("%d",&t);
        int l=strlen(s);
        while(t--)
        {
            scanf("%d",&x);
            ml=max(ml,l+x-1);
            int y=x;
            while((y=Find(y))<=l+x-1)
            {
                res[y]=s[y-x];
                fa[y]=y+1;
            }
        }
    }
    for(int i=1;i<=ml;i++)if(!res[i])res[i]=‘a‘;
    puts(res+1);
    return 0;
}

方法二:把所有点放进set容器里,拜访过的点就删掉。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
char s[N],res[N*2];
set<int>ss;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    for(int i=1;i<N*2;i++)ss.insert(i);
    int n,t,a;
    cin>>n;
    int ml=0;
    for(int i=0;i<n;i++)
    {
        cin>>s;
        cin>>t;
        int l=strlen(s);
        while(t--)
        {
            cin>>a;
            while(1)
            {
                set<int>::iterator it=ss.lower_bound(a);
                if(it==ss.end()||*it-a>=l)break;
                ml=max(ml,*it);
                res[*it]=s[*it-a];
                ss.erase(it);
            }
        }
    }
    for(int i=1;i<ml+1;i++)if(!res[i])res[i]=‘a‘;
    puts(res+1);
    return 0;
}
时间: 2024-08-29 22:21:32

Codeforces C - String Reconstruction的相关文章

CodeForces 828C String Reconstruction(并查集思想)

题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第一个没填涂的位置,那每次都能跳过涂过的地方.每次填完当前格就去填find(fa[i + 1]). ps:一定要合并,不然超时. 代码: #include<stack> #include<vector> #include<queue> #include<set>

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

LINK 题目大意 给你n个串和在原串中的出现位置,问原串 思路 直接跑肯定是GG 考虑怎么优化 因为保证有解,所以考虑过的点我们就不再考虑 用并查集维护当前每个点之后最早的没有被更新过的点 然后就做完了,很巧妙对吧 c++//Author: dream_maker #include<bits/stdc++.h> using namespace std; //---------------------------------------------- //typename typedef lo

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction

题意:给出各个字符串出现的起始位置,问整个的字符串是什么,(字典序最小) 思路:开始写的是用set+优先队列存取每个位置出现的最长字符串,然后遍历,爆内存...爆...内...存...我们可以用并查集,已经确认的位置他们并在一起,指向后面第一个没有被确认的(看代码理解吧) 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=2e6+10; 4 5 int n,fa[N]; 6 char s[N],b[N]; 7 8 in

CodeForces 159c String Manipulation 1.0

String Manipulation 1.0 Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 159C64-bit integer IO format: %I64d      Java class name: (Any) One popular website developed an unusual username editing proced

Codeforces 118A String Task

题目链接 http://codeforces.com/problemset/problem/118/A #include<iostream> #include<string> #include<cctype> #include<algorithm> using namespace std; int main() { string str; cin>>str; int i; //将代码变成小写 transform(str.begin(), str.

Round #423 C. String Reconstruction(Div.2)

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 re

Codeforces 1296E2 - String Coloring (hard version)

题目大意: 给定一段长度为n的字符串s 你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换 需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z) 你可以使用无限种颜色,但是要保证用到的颜色种类最少 从1开始对颜色进行编号,先输出最少使用的颜色种类,再给出涂色方案 解题思路 1: 可以引入一个 r 数组,开26个空间代表26种字母 这个数组 r[i] 的值代表 第 i 个字母在前面涂的颜色最大编号是多少,0表示没出现过 遍历这个字符串,再从当前字母后一个位置开始往后再遍历这个

codeforces 128B. String Problem (floyd预处理)

题意: 给两个字符串,以及m个从字符a变换到b的代价,问把两个字符串变得相同的最小代价 思路: 将字符串变换的代价当做一条有向边建图,之后跑floyd,得到从字符i变成j的最小代价dis[i][j] 之后遍历两个字符串的每一位,不相同的话枚举变成什么字符所需要的代价最小,如果找不到这样一个中间点使得s1[i]=ch,s2[i]=ch,则直接输出-1,最后将最小代价累加就好了 #include<iostream> #include<algorithm> #include<cst

Codeforces Round #423 (Div. 2) C 思维,并查集 或 线段树 D 树构造,水

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction   思维,并查集 或 线段树 题意:一个字符串被删除了,但给出 n条信息,要还原出可能的字典序最小的字符串.信息有:字符串ti,ki个位置xi,表明原本的字符串在xi位置是以字符串ti开头的. tags:惨遭 fst,一开始把所有字符串都存下来,排序做的,结果爆内存了.. 方法1: 考虑并查集,对于字符串 ti,在位置xi,