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;
    }
  • 相关阅读:
    I2S波形解析
    F407整点原子I2C波形解码
    WAVE格式文件说明
    ADC结构体初始化成员
    这次,我是真的想吐槽MDK
    I2S源程序(正点原子F407探索者)
    强制类型转换
    嵌套结构体的初始化
    lua 元方法 __index
    lua pairs 与 ipairs
  • 原文地址:https://www.cnblogs.com/wos1239/p/4398558.html
Copyright © 2011-2022 走看看