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;
    }
  • 相关阅读:
    杭电 FatMouse' Trade
    Navicat Report Viewer 设置选项的问题
    用Beyond Compare精确查找文本内容的方法
    Beyond Compare表格比较详解
    Navicat Report Viewer 如何连接到 MySQL 数据库
    Navicat for MySQL 选项设置技巧详解
    Navicat 用什么方法检测 Oracle 数据库安全性
    有哪些好用的比较工具
    Beyond Compare文件比较有哪些方式
    Beyond Compare怎样修改差异文件夹
  • 原文地址:https://www.cnblogs.com/wos1239/p/4398558.html
Copyright © 2011-2022 走看看