ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU1867 A + B for you again【KMP】

2019-02-05 22:50:16  阅读:342  来源: 互联网

标签:again HDU1867 int s2 s1 next k2 KMP asdf


A + B for you again

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9765 Accepted Submission(s): 2410

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
2008杭电集训队选拔赛——热身赛

问题链接HDU1867 A + B for you again
问题简述:(略)
问题分析
    给定2个字符串a和b,输出a+b或b+a中长度较短的字符串,若其长度相同则输出字典顺序较小者。其中的+运算定义为:a最长后缀和b最长前缀相等则合并。例如:abcdef+defppp=abcdefppp。
    实现方法是使用KMP算法分别计算,一是a后缀和b前缀的最长相同序列(考虑a+b),二是b后缀和a前缀的最长相同序列(考虑b+a)。然后,从中选出较长的序列输出结果,若长度相等则按字典顺序输出结果。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* HDU1867 A + B for you again */

#include <stdio.h>
#include <string.h>

#define N 100000
char s1[N + 1], s2[N + 1];
int next[N + 1];

void set_next(char s[], int next[], int len)
{
    int i, j;
    i = 0, j = next[0] = -1;
    while (i < len) {
        while (j != -1 && s[i] != s[j])
            j = next[j];
        next[++i] = ++j;
    }
}

int kmp(char *s, char *t)
{
    int i = 0, j = 0;
    int lens = strlen(s), lent = strlen(t);
    set_next(t, next, lent);
    while (i < lens && j < lent) {
        if (j == -1 || s[i] == t[j])
            i++, j++;
        else j = next[j];
    }
    return i == lens ? j : 0;
}

int main(void)
{
    while(scanf("%s%s", s1, s2) != EOF) {
        int k1 = kmp(s1, s2);
        int k2 = kmp(s2, s1);
        if(k1 > k2)
            printf("%s%s\n", s1, s2 + k1);
        else if(k1 < k2)
            printf("%s%s\n", s2, s1 + k2);
        else {
            if(strcmp(s1, s2) < 0)
                printf("%s%s\n", s1, s2 + k1);
            else
                printf("%s%s\n", s2, s1 + k2);
        }
    }

    return 0;
}

标签:again,HDU1867,int,s2,s1,next,k2,KMP,asdf
来源: https://www.cnblogs.com/tigerisland45/p/10353263.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有