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 }
  • 相关阅读:
    el-input 标签中密码的显示和隐藏
    java 使用RedisTemplate实现Redis事务
    mac 安装 Java 环境
    Snowflake 分布式UUID
    lsof 查看端口使用时刻
    nginx.pid" failed (2: No such file or directory)
    解决Redis之MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist
    Linux环境下 Jna 解决so依赖文件not found
    Ubuntu mysql 在线安装
    Linux中为什么执行自己的程序要在前面加./
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11375807.html
Copyright © 2011-2022 走看看