zoukankan      html  css  js  c++  java
  • Simpsons’ Hidden Talents(扩展KMP)

    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

    题意:

    求第一个字符串所有后缀中,匹配第二个前缀最长的那个,输出这个前缀和前缀长度

    思路:

    扩展KMP模板题

    千言万语尽在代码中:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int next[50005], extend[50005], len1, len2;
     5 char s[50005], t[50005];
     6 
     7 void get_next()
     8 {
     9     int a, p, i;
    10     a = 0;
    11     p = 0;
    12     next[0] = len2;
    13     for(i=1; i<len2; i++)
    14     {
    15         if(i>=p||i+next[i-a]>=p)
    16         {
    17             if(i>=p) p = i;
    18             while(p<len2&&s[p]==s[p-i]) p++;
    19             next[i] = p-i;
    20             a = i;
    21         }
    22         else next[i] = next[i-a];
    23     }
    24 }
    25 
    26 void exkmp()
    27 {
    28     get_next();
    29     int a, p, i;
    30     a = 0;
    31     p = 0;
    32     for(i=0; i<len1; i++)
    33     {
    34         if(i>=p||i+next[i-a]>=p)
    35         {
    36             if(i>=p) p = i;
    37             while(p < len1 && p-i < len2 && t[p] == s[p-i]) p++;
    38             extend[i] = p - i;
    39             a = i;
    40         }
    41         else extend[i] = next[i-a];
    42     }
    43 }
    44 
    45 int main()
    46 {
    47     int i, j;
    48     while(~scanf("%s %s", s, t))
    49     {
    50         len1 = strlen(t);
    51         len2 = strlen(s);
    52 
    53         exkmp();
    54         int maxx = 0;
    55         for(i=len1 - 1, j = 0; j < len2 ; j++, i--)
    56         {
    57             if(extend[i] == len1 - i)
    58             {
    59                 if(extend[i] > maxx) maxx = extend[i];
    60             }
    61         }
    62 
    63         for(i=0; i<maxx; i++)
    64         {
    65             printf("%c", s[i]);
    66         }
    67 
    68         if(maxx>0) printf(" ");
    69 
    70         printf("%d
    ", maxx);
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    Makefile:(实验)多个目标匹配时会采用最完整匹配的目标
    线程调度为什么比进程调度更少开销?
    关于makefile中自动产生依赖的理解
    makefile中重载与取消隐藏规则示例
    xargs命令的使用过程中一个小领悟:管道与xargs的差别
    CODING 远程办公 开工不断线
    【 ECUG 演讲分享】吴海黎:CODING 微服务架构演进之路
    张海龙:云时代企业研发人员需求与人才培养
    腾讯云大学 x CODING | DevOps 实战:Jenkins Docker
    腾讯云大学 x CODING | 敏捷开发与 DevOps 实战
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11375807.html
Copyright © 2011-2022 走看看