zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    多校第五场 归并排序+暴力矩阵乘+模拟+java大数&amp;记忆化递归
    Cocos2dx-Android 之Makefile通用高级写法
    出现异常 child-&gt;m_pParent == 0
    mybatis一对多关联查询——(九)
    mybatis一对一关联查询——(八)
    mybatis关联查询数据模型分析——(七)
    mybatis动态sql——(六)
    mybatis输入输出映射——(五)
    SqlMapConfig.xml全局配置文件介绍——(四)
    mybatis开发dao的方法——(三)
  • 原文地址:https://www.cnblogs.com/wmj6/p/10425149.html
Copyright © 2011-2022 走看看