http://acm.hdu.edu.cn/showproblem.php?pid=4763

http://codeforces.com/problemset/problem/126/B

这两个题都是在考察next的应用。区别在于一个是不能有重叠,一个是可以有重叠。

一级条件,前后缀都存在;二级条件 在之前存在前后缀。

对于可以重叠的,很简单,利用next记录一下前缀后缀都存在的那些点,然后跑一遍循环找一下这些满足一级条件的哪些满足二级条件,就ok了。

不可以重叠的,其实就是加了一个零级条件,判断长度,还有一个四级条件,还是长度。

还是有收获的。

对于next理解又多了一点,标注next其实就是标注这个位置之前是否满足前后缀一样。满足就美滋滋了。

贴代码,具体体会一下

 #include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(s1) memset(s1,0,sizeof(s1))
#define meminf(s1) memset(s1,0x3f,sizeof(s1))
#define ll long long
using namespace std;
int nex[],l1,l2;
void getn(int n,char c[])
{
int i=,j=-;
nex[]=-;
while(i<n)
{
if(j==-||c[i]==c[j])
{
i++;j++;nex[i]=j;
}
else j=nex[j];
}
return;
}
int f[];
int main()
{
int t;
cin>>t;
char s[];
while(t--)
{
scanf("%s",s);
int l=strlen(s);
getn(l,s);
mem0(f);
int tmp=l;
while(tmp>)
{
if(l>=*tmp) f[tmp]=;
tmp=nex[tmp];
}
int ans=;
for(int i=l-;i>;i--)
{
tmp=i;
while(tmp>){
if(f[tmp]&&i>=*tmp&&l>=tmp+i)
{
ans=max(ans,tmp);break;
}
tmp=nex[tmp];
}
}
cout<<ans<<endl;
}
}
 #include<bits/stdc++.h>
using namespace std;
#define mem0(a) memset(a,0,sizeof a)
#define ll long long
int nxt[],vis[];
inline void getn(int len,char s[])
{
int i,j;
nxt[]=-;
for (i=;i<len;++i)
{
j=nxt[i];
while (j!=-&&s[j]!=s[i]) j=nxt[j];
nxt[i+]=j+;
}
return;
}
int main()
{
char s[];
while(~scanf("%s",s))
{
int l=strlen(s);int ans=;
int flag=;
getn(l,s);
int maxx=nxt[l];
mem0(vis);
vis[maxx]=;
if(maxx==) puts("Just a legend\n");
else
{
while(maxx>){
vis[maxx]=;
maxx=nxt[maxx];
}
for(int i=;i<l;++i)
{
if(vis[nxt[i]]&&nxt[i]>ans)
ans=nxt[i],flag=;
}
if(!flag) puts("Just a legend\n");
else printf("%s\n",s+l-ans);
}
}
return ;
}

CF126B password&&HDU 4763 Theme Section的更多相关文章

  1. hdu 4763 Theme Section(KMP水题)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. HDU 4763 Theme Section

    题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...

  3. HDU 4763 Theme Section(KMP灵活应用)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. 2013长春网赛1005 hdu 4763 Theme Section(kmp应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题意:给出一个字符串,问能不能在该串的前中后部找到相同的子串,输出最长的字串的长度. 分析:km ...

  6. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

  7. HDU 4763 Theme Section ( KMP next函数应用 )

    设串为str, 串长为len. 对整个串求一遍next函数,从串结尾开始顺着next函数往前找<=len/3的最长串,假设串长为ans,由于next的性质,所以找到的串肯定满足E……E这种形式, ...

  8. HDU - 4763 Theme Section (KMP的next数组的应用)

    给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...

  9. hdu 4763 Theme Section(next数组找串中三段相等)

    题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...

随机推荐

  1. elasticsearch 7 安装

    elasticsearch 安装 操作系统:CentOS Linux release 7.4 elasticsearch:elasticsearch-7.1.1 es7+centos7 1.软件下载 ...

  2. redis 设置密码验证

    1.找到配置文件:如/etc/redis/redis.conf 2.找到以下内容 # requirepass foobared 3.修改为(redispassword是密码) requirepass ...

  3. yum仓库及配置

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于服务器需求,需要在公司内网搭建内网yum源. 搭建内网yum源需要分以下几个步骤,如下: 1. yum是什么 2. repo文件是什么 3. r ...

  4. 什么是redis缓存穿透, 缓存雪崩, 缓存击穿

    什么是redis? redis是一个非关系型数据库,相对于其他数据库而言,它的查询速度极快,且能承受的瞬时并发量非常的高.所以常常被用来存放网站的缓存,以减少主要数据库(如mysql)的服务器压力. ...

  5. devicemaps_init(mdesc)

    devicemaps_init的参数为machine_desc结构体.以s3c6410为例,在arch/arm/mach-s3c64xx/mach-smdk6410.c中使用上述宏声明machine_ ...

  6. HashMap的实现原理和底层数据结构

    看了下Java里面有HashMap.Hashtable.HashSet三种hash集合的实现源码,这里总结下,理解错误的地方还望指正 HashMap和Hashtable的区别 HashSet和Hash ...

  7. Intellij Idea 创建JavaWeb项目

    折腾Tomcat折腾了两个晚上,第一个晚上怎么都进不了Tomcat的首页,第二个晚上进去了,但是新建的Web项目,在浏览器中运行,总是 Error on Apache Tomcat: The requ ...

  8. Python linecache模块

    Table of Contents 1. linecache 1.1. 其它 2. 参考资料 linecache 今天分享一个python的小模块: linecache, 可以用它方便地获取某一文件某 ...

  9. data相关应用

    文案参考:HTML5中的data-*属性和jQuery中的.data()方法使用 data属性选择器 $("li[data-id='1']")//选择li元素中data-id属性等 ...

  10. easyui-combogrid必填为空时无法通过表单验证的问题

    在使用easyui-combogrid时,由于html解析出的格式是如下三层: <td> <input id="txcombo" class="easy ...