Codeforces Round #423 Div. 2 C-String Reconstruction(思维)

题目大意:告诉你n个字符串以及这些字符串在字符串s中出现的位置(x1,x2.....xn),要求在满足上述条件的情况下,求出字典序最小的字符串s。

解题思路:主要问题是,如果直接模拟是会超时的,比如vvvvvvvvvv 3 1 2 3这样就有大量重复(因为题目说了这些字符串位置不会相互矛盾,所以已经有了字符的地方可以不用管了),每次都重复了len-1的长度,如果这段字符串长度为1e6那很容易就超时了。所以这里添加一个pre记录上一次字符串的末尾位置(因为题目说了给出的位置时递增的),每次比较一下开始位置xi和pre+1的大小取较大的为起始的字符添加点。特意画了张丑图:

代码:

 1 #include<stdio.h>
 2 #include<cstring>
 3 const int N=2e6+5;
 4
 5 char tmp[N];
 6 char s[N];
 7 int idx[N];
 8
 9 int max(int a,int b){
10     return a>b?a:b;
11 }
12
13 int main(){
14     memset(s,‘#‘,sizeof(s));
15     int n,mlen=-1;
16     scanf("%d",&n);
17     int num=0;
18     for(int i=1;i<=n;i++){
19         int m,pre=-1;//pre记录上一次字符串的末尾位置
20         scanf("%s %d",tmp,&m);
21         int len=strlen(tmp)-1;
22         while(m--){
23             int pos,x;
24             scanf("%d",&x);
25             mlen=max(mlen,x+len);
26             pos=max(pre+1,x);
27             for(int j=pos;j<=x+len;j++){
28                 s[j]=tmp[j-pos];
29                 num++;
30             }
31             //存储上一次末尾位置
32             pre=x+len;
33         }
34     }
35     for(int i=1;i<=mlen;i++){
36         if(s[i]==‘#‘)
37             s[i]=‘a‘;
38     }
39     s[mlen+1]=‘\0‘;
40     printf("%s\n",s+1);
41 }
时间: 2024-08-29 10:37:12

Codeforces Round #423 Div. 2 C-String Reconstruction(思维)的相关文章

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,

Codeforces Round #402 (Div. 2) D. String Game

题目链接:Codeforces Round #402 (Div. 2) D. String Game 题意: 给你两个字符串a,b,然后给你n=strlen(a)个数字n1,n2,...,nn,表示依次删a[ni-1]个字符. 当a串删到有k(k任意)个子串组合起来(顺序不变)刚好等于b串时,就不能删了. 比如 abba->(ab a) 刚好包括 aba ,bba不包括aab. 问最多能删多少次. 题解: 最开始还以为是要用某种数据结构啥的,结果都想复杂了,直接二分答案就行. 然后线性判断删掉后

Codeforces Round #423 (Div. 2) D. High Load(构造题)

题目链接:Codeforces Round #423 (Div. 2) D. High Load 题意: 给你一个数n和k,让你构造出一颗树,有k个叶子节点,使得这棵树的任意两个点的距离的最大值最小. 题解: 显然要使得这棵树的任意两个点的距离的最大值最小,每个点离树根越近越好. 然后要求有k个叶子节点,所以我就任意选一个点为根,然后构成一棵m叉的树就行了. 最大距离的最小值就是离根最远的和最近的加一加就行了. 1 #include<cstdio> 2 #define F(i,a,b) for

Codeforces Round #423 (Div. 2) A-C

A. Restaurant Tables 这里看错题意还wa了两发.... 按题意模拟就行了 水题 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #includ

Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)

A.String Reconstruction B. High Load C. DNA Evolution 题意:给定一个只包含A,T,C,G的字符串S,有如下两种操作 1)修改一个点的字母. 2)给定一个字符串e ($\left | e \right |\leq 10$),生成一个由e重复组成的新串,eee...,问$S_{l..r}$中有几个字母跟这个新的字符串一一对应. SOL:对于每个字母,用BIT[x][y][L]表示$S_{1..L}$中,所有$\equiv x\left (mod

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 Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组

E. DNA Evolution time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seate

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)D. High Load

题意:出n个点,其中k个叶子节点,问构造出的树最远的两个点最近是多少 思路:以一个点为中心,然后m个伸出,一层层扩散,(n-1)%m==k,如果k==0,即可以平分,长度就是2*(n-1)/m,如果取模为k==1,说明多出一个,+1,其他的话,就是最后一层补k个,但是最长的还是+2 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main(){ 5 int n,m; 6 cin>>n>>m; 7 int