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. BZOJ3679: 数字之积(数位dp)

    题意 题目链接 Sol 推什么结论啊. 直接大力dp,$f[i][j]$表示第$i$位,乘积为$j$,第二维直接开map 能赢! /* */ #include<iostream> #inc ...

  2. 【JS】JS实现时间戳转换成普通时间

    var time = 1514457627; alert(getDate(time)); function getDate(tm){ var tt=new Date(parseInt(tm) * 10 ...

  3. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...

  4. 按时按登录IP记录Linux所有用户操作日志的方法(附脚本)

    PS:Linux用户操作记录一般通过命令history来查看历史记录,但是如果因为某人误操作了删除了重要的数据,这种情况下history命令就不会有什么作用了.以下方法可以实现通过记录登陆IP地址和所 ...

  5. 3 View - Request对象

    1.HttpReqeust对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象 视图函数的第一个参数是HttpRequest对象 在django.http模块中定义了Htt ...

  6. Go语言之并发编程(四)

    同步 Go 程序可以使用通道进行多个 goroutine 间的数据交换,但这仅仅是数据同步中的一种方法.通道内部的实现依然使用了各种锁,因此优雅代码的代价是性能.在某些轻量级的场合,原子访问(atom ...

  7. loj2063 「HAOI2016」字符合并

    ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  8. C# 调试之 Debug.WriteLine()、Trace.WriteLine()

    Trace 类 和 Debug 类的区别在于,Trace 类会同时在 Debug.Release 模式下起作用,而 Debug 只作用在 Debug 模式下. 区别: 1. 输出跟踪信息 Trace. ...

  9. Mac OS X 上的Apache配置

    Mac系统自带apache服务器 查看apache版本 sudo apachectl -v 启动apache sudo apachectl start 重启apache sudo apachectl ...

  10. 非旋Treap总结 : 快过Splay 好用过传统Treap

    非旋$Treap$ 其高级名字叫$Fhq\ Treap$,既然叫$Treap$,它一定满足了$Treap$的性质(虽然可能来看这篇的人一定知道$Treap$,但我还是多说几句:$Fhp\ Treap$ ...