• hdu 2594 Simpsons’ Hidden Talents(扩展kmp)


    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
    题意:找字符串a和字符串b的最长公共前后缀
    #include <cstdio>
    #include <map>
    #include <iostream>
    #include<cstring>
    #include<bits/stdc++.h>
    #define ll long long int
    #define M 6
    using namespace std;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
    int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
    const int inf=0x3f3f3f3f;
    const ll mod=1e9+7;
    int nextt[50007];
    int extend[50007];
    void getnext(string s){
        int po; int len=s.length();
        nextt[0]=len; //初始化next[0]
        int pos=0;
        while(s[pos]==s[pos+1]&&pos<len-1) pos++; 
        nextt[1]=pos; //计算next[1]
        po=1; //初始化po的位置
        for(int i=2;i<len;i++){
            if(nextt[i-po]+i<po+nextt[po]) //第一种情况,可以直接得到next[i]的值
                nextt[i]=nextt[i-po];
            else{ //第二种情况,要继续匹配才能得到next[i]的值
                int j=po+nextt[po]-i;
                if(j<0) j=0;
                while(i+j<len&&s[j]==s[i+j]) j++;
                nextt[i]=j;
                po=i;
            }
            //cout<<nextt[i]<<endl;
        }
    }
    void exkmp(string a,string b){
        int len1=a.length(); int len2=b.length();
        getnext(a);
        int po; int pos=0;
        while(a[pos]==b[pos]&&pos<len1&&pos<len2) pos++;
        extend[0]=pos;
        po=0;
        for(int i=1;i<len2;i++){
            if(nextt[i-po]+i<extend[po]+po)
                extend[i]=nextt[i-po];
            else{
                int j=po+extend[po]-i;
                if(j<0) j=0;
                while(i+j<len2&&a[j]==b[i+j]&&j<len1) j++;
                extend[i]=j;
                po=i;
            }
        }
    }
    int main(){
        ios::sync_with_stdio(false);
        string s1,s2;
        while(cin>>s1>>s2){
            exkmp(s1,s2);
            int len1=s1.length(); int len2=s2.length();
            int maxn=-inf;
            int maxn_i;
            for(int i=0;i<len2;i++){
                //cout<<extend[i]<<endl;
                if(extend[i]+i==len2&&maxn<extend[i]){
                    maxn=extend[i];
                    maxn_i=i;
                }
            }
            if(maxn==-inf) cout<<"0"<<endl;
            else{
                
                for(int i=maxn_i;i<maxn_i+maxn;i++)
                    cout<<s2[i];
                    cout<<" "<<maxn;
                cout<<endl;
            }
        } 
        return 0;
    }
  • 相关阅读:
    IIS负载均衡Application Request Route详解第二篇:创建与配置Server Farm
    负载均衡原理与实践详解 第一篇(重新整理)
    负载均衡原理剖析与实践第一篇介绍篇
    查询优化器内核剖析第八篇:执行引擎之数据访问操作Seek(上)
    提高ASP.NET性能与可伸缩性的几个个常用方法剖析
    IIS负载均衡Application Request Route详解第三篇:使用ARR进行Http请求的负载均衡(上)
    负载均衡第二篇负载均衡基础知识普及
    查询优化器内核剖析第七篇:执行引擎之数据访问操作Scan
    活在当下:人这一辈子,你总的坚持点什么
    LINQ 的演变及其对 C# 设计的影响
  • 原文地址:https://www.cnblogs.com/wmj6/p/10425149.html
Copyright © 2020-2023  润新知