当前位置: 首页 > 工具软件 > Riemann > 使用案例 >

riemann

吕新
2023-12-01

Problem Description

Homer: Marge, I just figured out away to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics,OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the lengthof the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being apolitician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix ofs1 that is a suffix of s2.

Input

Input consists of two lines. Thefirst line contains s1 and the second line contains s2. You may assume allletters are in lowercase.

Output

Output consists of a single linethat contains the longest string that is a prefix of s1 and a suffix of s2,followed by the length of that prefix. If the longest such string is the emptystring, then the output should be 0.
The lengths of s1 and s2 will be at most 50000.

Sample Input

clinton
homer
riemann
marjorie

Sample Output

0
rie 3

给出两个字符串,求出第一个字符串的前缀和第二个字符串的后缀最大的相等的部分及长度

将字符串b连接到字符串a上面,这样直接得到next数组输出最后一个next数组的值即可,但要注意特殊情况,如aaa aaaaa 或者aaaaaa aaa 等,所以要先判断字符串a,b的长度,当链接之后最后一个next数组的值大于min,则归为另一种情况输出

易错分析

注意两个字符串一样连接后导致的前后缀长度增加的情况


#include<stdio.h>
#include<string.h>
char a[100010];
int next[100010],lena,min;
void get_next()
{
    int i=1,j=0;
    next[0]=0;
    while(i<lena)
    {
        if(j==0 && a[i]!=a[j])
        {
            next[i]=0;
            i++;
        }
        else if(j>0 && a[i]!=a[j])
            j=next[j-1];
        else
        {
            next[i]=j+1;
            i++;
            j++;
        }
    }
//    for(i=0;i<lena;i++)
//        printf("%d ",next[i]);
//    printf("\n");
    if(next[lena-1]==0)
        printf("0\n");
    else
    {
        if(next[lena-1]>min)
        {
            for(i=lena-min;i<lena;i++)
                printf("%c",a[i]);
            printf(" %d\n",min);
        }
        else
        {
            for(i=lena-next[lena-1];i<lena;i++)
                printf("%c",a[i]);
            printf(" %d\n",next[lena-1]);    
        }
    }
}
int main()
{
    char b[50010];
    int lenb,len;
    while(scanf("%s",a)!=EOF)
    {
        scanf("%s",b);
        lenb=strlen(b);
        len=strlen(a);
        min=len<lenb?len:lenb;
        //printf("%d\n",min);
        strcat(a,b);
        lena=strlen(a);
        get_next();
    }
    return 0;
}


 类似资料:

相关阅读

相关文章

相关问答