R - Simpsons’ Hidden Talents

题目描述:

Homer: Marge, I just figured out a way 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 length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician 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 of s1 that is a suffix of s2.
Input
Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
Output
Output consists of a single line that 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 empty string, 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
题意:前面说了一大推废话,其实题意很简单。就是给你两个字符串,让你求出第一个字符串的前缀是否是第二个字符串的后缀。这里的前缀长度是不定的,但输出是要输出最长的满足条件的前缀。
分析:本题需要用到next数组的意义。next数组的最后一个值是可表示这个字符串的后缀和这个字符串前缀相同的字符长度。那么到这个时候,就可以想到我们只要把两个字符串连接起来,在求next数组就可以了。
到现在还有一个问题,就是连接起来求得next数组是有可能大于第二个或第一个字符串长度的,那么这很明显是不符合题意的。
例如:
abcabc
abc
这两个字符串连接后的next数组是:
-1 0 0 0 1 2 3 4 5 6(注意:这个求next数组是并未有优化步骤)
我们可以发现6大于了第二个字符串的长度,那么此时怎么办。
同样可以放现next[6]是等于3的。正好3符合题意的。到了这个时候我们就可以写出代码了
ac代码:

#include"stdio.h"
#include"string.h"
void Get_next(char text[],int next[])
{
    int i,j,len;
    len=strlen(text);
    i=0;
    j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(j==-1||text[i]==text[j])
        {
            i++;
            j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}

int main()
{
    char text[100001],word[50001],T[50001];
    int next[100001];
    int i,j,k;
    while(~scanf("%s%s",text,word))
    {
        i=strlen(text);
        k=strlen(word);
        strcat(text,word);
        int len=strlen(text);
        Get_next(text,next);
       while(next[len]>i||next[len]>k)
            len=next[len];
        if(len==-1||next[len]==0)
            printf("%d\n",0);
        else
        {
            text[next[len]]='\0';
            printf("%s %d\n",text,next[len]);
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值