zoukankan      html  css  js  c++  java
  • hdu2594 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.

    InputInput consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.OutputOutput 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的next数组一步到位,刚开始wa了一发,冷静了一下发现要是结果大于两个的最小长度,就会wa。。。。
    例如:aaaa
    aaa
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    using namespace std;
    
    const double g=10.0,eps=1e-9;
    const int N=100000+5,maxn=60+5,inf=0x3f3f3f3f;
    
    int Next[N],slen;
    string str,ptr;
    
    void getnext()
    {
        int k=-1;
        Next[0]=-1;
        for(int i=1;i<slen;i++)
        {
            while(k>-1&&str[k+1]!=str[i])k=Next[k];
            if(str[k+1]==str[i])k++;
            Next[i]=k;
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
     //   cout<<setiosflags(ios::fixed)<<setprecision(2);
        while(cin>>ptr>>str){
            int minn=min(ptr.size(),str.size());
            str=ptr+str;
            slen=str.size();
            getnext();
            int ans=Next[slen-1]+1;
            if(ans>minn)ans=minn;
            if(ans==0)cout<<0<<endl;
            else cout<<ptr.substr(0,ans)<<" "<<ans<<endl;
        }
        return 0;
    }
    View Code


  • 相关阅读:
    求大神回答这个管理系统不知道为啥不成功急!
    这个函数到底什么意思如何调用
    判断浮点数是否为零的问题
    字符串与列表的 常用方法
    变量名命名规范 运算符 流程控制
    ACM C++
    struts s:iterator循环遍历数据 自动生成序号
    JAVA将一个EXCEL多行订单产品字符串分解成一个个子订单 +连接符连接
    JS在HTML中获取到所有选中的checkbox的值
    自己做的java-WEB项目。希望360浏览器能够默认使用极速模式打开
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6818735.html
Copyright © 2011-2022 走看看