HDU1867 A + B for you again【KMP】

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;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10353263.html

时间: 2024-07-29 15:21:16

HDU1867 A + B for you again【KMP】的相关文章

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote

【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合. 题目思路: [KMP][最小表示法] 循环同构问题.可以写KMP,我懒得写KMP了就写了循环同构的最小表示法. 首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配. A

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]

HDU3746 Cyclic Nacklace 【KMP】

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2538    Accepted Submission(s): 1154 Problem Description CC always becomes very depressed at the end of this month, he has checke

NYOJ327 亲和串 【KMP】

亲和串 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 最近zyc遇到了一个很棘手的问题:判断亲和串,以前判断亲和串的时候直接可以看出来,但现在不同了,现在给出的两字符串都非常的大,看的zyc头都晕了.于是zyc希望大家能帮他想一个办法来快速判断亲和串.亲和串定义:给定两个字符串s1和s2,如果能通过s1循环移动,使s2包含在s1中,那么我们就说s2是s1的亲和串. 输入 本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的

HDU4763 Theme Section 【KMP】

Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1114    Accepted Submission(s): 579 Problem Description It's time for music! A lot of popular musicians are invited to join us in t

HDU2594 Simpsons’ Hidden Talents 【KMP】

Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2798    Accepted Submission(s): 1055 Problem Description Homer: Marge, I just figured out a way to discover some of the

【KMP】OKR-Periods of Words

[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个空串,那么我们说P是A的一个proper前缀.定义Q是A的周期,当且仅当Q是A的一个proper前缀并且A是QQ的前缀(不一定要是proper前缀).比如串abab和ababab都是串abababa的周期.串A的最大周期就是它最长的一个周期或者是一个空串(当A没有周期的时候),比如说,ababab的