zoukankan      html  css  js  c++  java
  • [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher J

    J - Simpsons’ Hidden Talents HDU - 2594

    题目链接:https://vjudge.net/contest/70325#problem/J

    题目:

    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
    题意:给你两个字符串,求出第一个字符串的一个前缀,且这个前缀是第二个字符串的后缀,如果不存在,输出-1;
    思路:可以再建立个字符串,把这两个字符串合并,中间用一个!来连接,防止相连前缀的情况,然后nextt[len]就是这个拼起来的字符串的相等的前后缀了,len就是这个拼的字符串的长度,
    // 
    // Created by HJYL on 2019/8/16.
    //
    #include <iostream>
    #include <vector>
    #include <map>
    #include <string>
    #include <queue>
    #include <stack>
    #include <set>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+10;
    char str[maxn],str1[maxn],str2[maxn];
    int nextt[maxn];
    void getnext()
    {
        int i=0,j=-1;
        nextt[0]=-1;
        int len=strlen(str2);
        while(i<len)
        {
            if(j==-1||str2[i]==str2[j])
            {
                i++,j++;
               // if(str2[i]!=str2[j])
                    nextt[i]=j;
              //  else
                   // nextt[i]=nextt[j];
            } else
                j=nextt[j];
        }
    }
    
    int main()
    {
       // freopen("C:\Users\asus567767\CLionProjects\untitled\text","r",stdin);
        while(~scanf("%s%s",str,str1))
        {
            int len=strlen(str);
            int len1=strlen(str1);
            int pos=0;
            for(int i=0;i<len;i++)
                str2[pos++]=str[i];
            str2[pos++]='!';
            for(int i=0;i<len1;i++)
                str2[pos++]=str1[i];
            getnext();
    //        cout<<len2<<"=len2"<<endl;
    //        cout<<nextt[len2]<<"hhh"<<endl;
    //        cout<<"str2="<<str2<<endl;
            if(nextt[pos]<=0)
                printf("0
    ");
            else {
                for (int i = 0; i < nextt[pos]; i++)
                    printf("%c", str2[i]);
                printf(" %d
    ", nextt[pos]);
            }
        }
        return 0;
    }
  • 相关阅读:
    C#前端画表
    JavaScript中遍历获取Json中属性值
    【数据库】Redis(5)--Redis发布订阅
    【java框架】SpringBoot(9) -- Springboot中如何优雅的进行字段校验
    【开发工具】-- IDEA集成Git在实际项目中的运用
    【Java代码之美】 -- Java必会的工具库,减少代码量的利器
    【java框架】SpringMVC(4) -- JSON相关的数据返回
    聊聊基于jdk实现的spi如何与spring整合实现依赖注入
    聊聊springcloud项目同时存在多个注册中心客户端采坑记
    feign请求返回值反序列LocalDateTime异常记录
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11365307.html
Copyright © 2011-2022 走看看