• 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]='';
    40     printf("%s
    ",s+1);
    41 }
  • 相关阅读:
    计算机网络
    计算机网络知识总结
    final,static,super,this
    ArrayList
    基础面试题
    BATJ都爱问的多线程面试题
    Session过期、失效时间
    类加载器ClassLoader源码解析
    连接ftp服务器 JDK 1.7
    根据当前请求的特征,判断该请求是否来自手机终端
  • 原文地址:https://www.cnblogs.com/fu3638/p/7156414.html
Copyright © 2020-2023  润新知