zoukankan      html  css  js  c++  java
  • HDU 2594 Simpsons’ Hidden Talents (KMP——前缀后缀最长公共串)

    Simpsons’ Hidden Talents

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


    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
     
    题意:这题就是求第一个字符串的前缀和第二个字符串的后缀的最长公共字串,不存在输出0
    分析:这题和POJ 2752差不多,都是求前缀和后缀的公共串,这题就是把两个字符串连一起就行了,要注意,如果nex[n]超过一个串的长度的话,输出短的字符串。
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    #define FIN freopen("in.txt","r",stdin)
    using namespace std;
    const int MAXN=50000+5;
    char str[MAXN*2],b[MAXN];
    int nex[MAXN*2];
    int n;
    
    void getnext()
    {
        int j=0,k=-1;
        nex[0]=-1;
        while(j<n)
        {
            if(k==-1 || str[j]==str[k])
                nex[++j]=++k;
            else
                k=nex[k];
        }
    }
    
    int main()
    {
        //FIN;
        while(scanf("%s %s",str,b)!=EOF)
        {
            int len1=strlen(str),len2=strlen(b);
    
            strcat(str,b);
            n=strlen(str);
            getnext();
            while(nex[k]>len1 || nex[k]>len2)
                k=nex[k];
    
            if(nex[n]==0) printf("0
    ");
            else
            {
                for(int i=0;i<len;i++)
                    printf("%c",str[i]);
                printf(" %d
    ",len);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    获取有关控件的坐标
    Android PopupWindow的使用和分析
    Android RecyclerView 使用完全解析 体验艺术般的控件
    TextView中显示价格并且中间直接有一个横线
    Android Studio
    移动开发】Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewFlow)
    Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
    理解Java的IO流 2015年8月6日 21:30:38
    算法导论学习随笔——第七章 快速排序
    oj 1031 random permutation
  • 原文地址:https://www.cnblogs.com/clliff/p/4725494.html
Copyright © 2011-2022 走看看