zoukankan      html  css  js  c++  java
  • KMP--Simpsons’ Hidden Talents

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/C

    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
     
    题意: 给了两个字符串s1和s2,求s1是s2的后缀的最大长度。
     
    思路: 利用KMP算法,求出s1的next[]数组,用s1串和s2从s2[0]一直到s2串尾进行匹配,当匹配到s2串尾时的k值就是最大长度。
     

    next数组的求解思路:

      最重要的就是如何根据待匹配的模版字符串求出对应每一位的最大相同前后缀的长度。

    复制代码
     1 void makeNext(const char P[],int next[])
     2 {
     3     int q,k;//q:模版字符串下标;k:最大前后缀长度
     4     int m = strlen(P);//模版字符串长度
     5     next[0] = 0;//模版字符串的第一个字符的最大前后缀长度为0
     6     for (q = 1,k = 0; q < m; ++q)//for循环,从第二个字符开始,依次计算每一个字符对应的next值
     7     {
     8         while(k > 0 && P[q] != P[k])//递归的求出P[0]···P[q]的最大的相同的前后缀长度k
     9             k = next[k-1];          //不理解没关系看下面的分析,这个while循环是整段代码的精髓所在,确实不好理解  
    10         if (P[q] == P[k])//如果相等,那么最大相同前后缀长度加1
    11         {
    12             k++;
    13         }
    14         next[q] = k;
    15     }
    16 } 
    复制代码

       while循环所做的工作:

    1.   已知前一步计算时最大相同的前后缀长度为k(k>0),即P[0]···P[k-1];
    2.   此时比较第k项P[k]与P[q],如图1所示
    3.   如果P[K]等于P[q],那么很简单跳出while循环;
    4.   如果不等呢?那么我们应该利用已经得到的next[0]···next[k-1]来求P[0]···P[k-1]这个子串中最大相同前后缀,可能有同学要问了——为什么要求P[0]···P[k-1]的       最大相同前后缀呢???是啊!为什么呢? 原因在于P[k]已经和P[q]失配了,而且P[q-k] ··· P[q-1]又与P[0] ···P[k-1]相同,看来P[0]···P[k-1]这么长的子串是用不        了了,那么我要找个同样也是P[0]打头、P[k-1]结尾的子串即P[0]···P[j-1](j==next[k-1]),看看它的下一项P[j]是否能和P[q]匹配。如图2所示
    5.  
     
    本题代码:
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    char s1[50005],s2[50005];
    int nex[50005];
    
    void nnext(int len)
    {   ///求s1串的nex[]数组;
        int k=0;
        nex[0]=0;
        for(int i=1;i<len;i++)
        {   ///求出nex[]的每一个值;
            ///nex[i]表示以s1[i]结尾的字符串能与s1[]前缀匹配的最大长度;
            while(k>0&&s1[k]!=s1[i])
                k=nex[k-1];
            if(s1[k]==s1[i])
                k++;
            nex[i]=k;
        }
    }
    
    int main()
    {
        int k,len1,len2;
        while(scanf("%s%s",s1,s2)!=EOF)
        {
            k=0;
            len1=strlen(s1);
            len2=strlen(s2);
            nnext(len1);
            for(int i=0;i<len2;i++)
            {
                while(k>0&&s1[k]!=s2[i])
                    k=nex[k-1];
                if(s1[k]==s2[i])
                    k++;
                if(i==len2-1) break;
                if(k>len1-1)
                {
                    k=nex[k-1];
                }
            }
            if(k)
            {
                s1[k]='';
                printf("%s %d
    ",s1,k);
            }
            else
            printf("0
    ");
        }
        return 0;
     }
  • 相关阅读:
    delphi TOpenDialog
    delphi TSaveDialog
    Delphi中一些常用的组合键值
    delphi TColorDialog
    Delphi TFindDialog TReplaceDialog对话框在Memo中的使用
    Delphi ListView基本用法大全
    地球帝国3
    五笔难拆字字根表
    Delphi Format中的换行符号是什么
    myNote
  • 原文地址:https://www.cnblogs.com/chen9510/p/5306095.html
Copyright © 2011-2022 走看看