zoukankan      html  css  js  c++  java
  • hdu1867A + B for you again

    Problem Description
    Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is the tail substring of “asdf” and the head substring of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions.
     
    Input
    For each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty.
     
    Output
    Print the ultimate string by the book.
     
    Sample Input
    asdf sdfg asdf ghjk
     
    Sample Output
    asdfg asdfghjk
     
    Author
    Wang Ye
     
    Source
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    
    using namespace std;
    
    char s1[110000],s2[110000],s3[210000],s4[210000];
    int nextt[110000];
    int match[110000];
    
    
    int main()
    {
        int i,j,k;
        while(scanf("%s%s",s1+1,s2+1)!=EOF)
        {
            int l1 = strlen(s1+1),l2 = strlen(s2+1);
            nextt[1] = 0;
            for(i = 2;i<=l2;i++)
            {
                int t = nextt[i-1];
                while(t&&s2[i]!=s2[t+1]) t = nextt[t];
                if(s2[i] == s2[t+1]) t++;
                nextt[i] = t;
            }
            match[0] = 0;
            for(i = 1;i<=l1;i++)
            {
                int t = match[i-1];
                while(t&&s1[i]!=s2[t+1]) t = nextt[t];
                if(s1[i] == s2[t+1]) t++;
                match[i] = t;
            }
            int t1 = match[l1];
            for(i = 1;i<=l1;i++)
                s3[i] = s1[i];
            for(i = l1+1;i<=l1+l2-t1+1;i++)
                s3[i] = s2[i-l1+t1];
            nextt[1] = 0;
            for(i = 2;i<=l1;i++)
            {
                int t = nextt[i-1];
                while(t&&s1[i]!=s1[t+1]) t = nextt[t];
                if(s1[i] == s1[t+1]) t++;
                nextt[i] = t;
            }
            match[0] = 0;
            for(i = 1;i<=l2;i++)
            {
                int t = match[i-1];
                while(t&&s2[i]!=s1[t+1]) t = nextt[t];
                if(s2[i] == s1[t+1]) t++;
                match[i] = t;
            }
            int t2 = match[l2];
            for(i = 1;i<=l2;i++)
                s4[i] = s2[i];
            for(i = l2+1;i<=l2+l1-t2+1;i++)
                s4[i] = s1[i-l2+t2];
            if(t1>t2) printf("%s",s3+1);
            else if(t2>t1) printf("%s",s4+1);
            else
            {
                int l = l1+l2-t2;
                bool bb = 0;
                for(i = 1;i<=l;i++)
                {
                    if(s3[i]<s4[i])
                    {
                        printf("%s",s3+1);
                        bb = 1;
                        break;
                    }
                    else if(s3[i]>s4[i])
                    {
                        printf("%s",s4+1);
                        bb = 1;
                        break;
                    }
                }
                if(!bb) printf("%s",s3+1);
            }
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    MySQL数据库安装和基本使用
    Android studio JNI技术实现与本地C++链接
    笔记 java中i++ 和 ++i的区别
    LaTeX中添加usepackage{subfigure}一直报错的解决办法,亲测
    Android Studio 出现“Cannot resolve symbol” 解决办法
    记录一个sql
    linux 文件内容查找、去重、统计
    MVC框架的模板缓存
    Linux 命令学习
    笔记 :PHP中的数据结构库SPL
  • 原文地址:https://www.cnblogs.com/wos1239/p/4398558.html
Copyright © 2011-2022 走看看