• hdu2597 Simpsons’ Hidden Talents


    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2594

    题目:

    Simpsons’ Hidden Talents

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8709    Accepted Submission(s): 3051


    Problem Description
    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
     
    Source
     
    Recommend
    lcy
     思路:裸扩展kmp
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 const int MM=100005;
     7 int nt[MM],extand[MM];
     8 char S[MM],T[MM];
     9 void Getnext(const char *T){
    10      int len=strlen(T),a=0;
    11      nt[0]=len;
    12      while(a<len-1 && T[a]==T[a+1]) a++;
    13      nt[1]=a;
    14      a=1;
    15      for(int k=2;k<len;k++){
    16          int p=a+nt[a]-1,L=nt[k-a];
    17          if( (k-1)+L >= p){
    18              int j = (p-k+1)>0 ? (p-k+1) : 0;
    19              while(k+j<len && T[k+j]==T[j]) j++;
    20              nt[k]=j;
    21              a=k;
    22          }
    23          else
    24              nt[k]=L;
    25      }
    26 }
    27 void GetExtand(const char *S,const char *T){
    28      Getnext(T);
    29      int slen=strlen(S),tlen=strlen(T),a=0;
    30      int MinLen = slen < tlen ? slen : tlen;
    31      while(a<MinLen && S[a]==T[a]) a++;
    32      extand[0]=a;
    33      a=0;
    34      for(int k=1;k<slen;k++){
    35          int p=a+extand[a]-1, L=nt[k-a];
    36          if( (k-1)+L >= p){
    37              int j= (p-k+1) > 0 ? (p-k+1) : 0;
    38              while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
    39              extand[k]=j;
    40              a=k;
    41          }
    42          else
    43              extand[k]=L;
    44      }
    45 }
    46 int main()
    47 {
    48     while(scanf("%s%s",S,T)==2)
    49     {
    50         GetExtand(T,S);
    51         int len=strlen(T),mx=0,st;
    52         for(int i=0;i<len;i++)
    53         if(extand[i]>mx&&extand[i]==len-i) mx=extand[i],st=i;
    54         for(int i=0;i<mx;i++)
    55             printf("%c",S[i]);
    56         if(mx)
    57             printf(" ");
    58         printf("%d
    ",mx);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    ABP入门系列(10)——扩展AbpSession
    Asp.net mvc 知多少(五)
    ABP入门系列(9)——权限管理
    ABP入门系列(8)——Json格式化
    Asp.net mvc 知多少(四)
    SQL递归查询知多少
    ABP入门系列(7)——分页实现
    课程四(Convolutional Neural Networks),第三 周(Object detection) —— 0.Learning Goals
    课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) ——3.Programming assignments : Residual Networks
    keras中的mini-batch gradient descent (转)
  • 原文地址:https://www.cnblogs.com/weeping/p/6669777.html
Copyright © 2020-2023  润新知