zoukankan      html  css  js  c++  java
  • hdu 1867 A + B for you again

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=1867

    A + B for you again

    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

    kmp。。。

    注意: 字符串A+B也可是B+A反正输出相加之后最短的那个,若相加之后长度相等输出字典序最小的那个。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<map>
     8 using std::cin;
     9 using std::cout;
    10 using std::endl;
    11 using std::find;
    12 using std::sort;
    13 using std::map;
    14 using std::pair;
    15 using std::vector;
    16 using std::multimap;
    17 #define pb(e) push_back(e)
    18 #define sz(c) (int)(c).size()
    19 #define mp(a, b) make_pair(a, b)
    20 #define all(c) (c).begin(), (c).end()
    21 #define iter(c) decltype((c).begin())
    22 #define cls(arr,val) memset(arr,val,sizeof(arr))
    23 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    24 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    26 const int N = 100010;
    27 typedef unsigned long long ull;
    28 int next[N];
    29 char str1[N], str2[N];
    30 struct KMP {
    31     int i, j, n, m;
    32     inline void get_next(char *src) {
    33         m = strlen(src);
    34         for (i = 1, j = next[0] = 0; i < m; i++) {
    35             while (j > 0 && src[i] != src[j]) j = next[j - 1];
    36             if (src[i] == src[j]) j++;
    37             next[i] = j;
    38         }
    39     }
    40     inline int kmp_match(char *text, char *pat) {
    41         n = strlen(text);
    42         for (i = j = 0; i < n; i++) {
    43             while (j > 0 && text[i] != pat[j]) j = next[j - 1];
    44             if (text[i] == pat[j]) j++;
    45         }
    46         return j;
    47     }
    48 }go;
    49 int main() {
    50 #ifdef LOCAL
    51     freopen("in.txt", "r", stdin);
    52     freopen("out.txt", "w+", stdout);
    53 #endif
    54     int p, q;
    55     while (~scanf("%s %s", str1, str2)) {
    56         go.get_next(str2);
    57         p = go.kmp_match(str1, str2);
    58         go.get_next(str1);
    59         q = go.kmp_match(str2, str1);
    60         if (p > q || (p == q && -1 == strcmp(str1, str2))) {
    61             printf("%s%s
    ", str1, str2 + p);
    62         } else {
    63             printf("%s%s
    ", str2, str1 + q);
    64         }
    65     }
    66     return 0;
    67 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    9、二叉排序树的创建、插入、遍历
    8、判断两个链表是否相交
    7、判断链表是否有环
    6、查找单链表中倒数第n个节点
    5、单链表的反转
    4、KMP(看毛片)算法
    3、希尔排序
    2、快速排序
    json字符串转为json对象
    json对象转化为字符串过程分析
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4625284.html
Copyright © 2011-2022 走看看