CF126B password&&HDU 4763 Theme Section
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的更多相关文章
- hdu 4763 Theme Section(KMP水题)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 4763 Theme Section
题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...
- HDU 4763 Theme Section(KMP灵活应用)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 2013长春网赛1005 hdu 4763 Theme Section(kmp应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题意:给出一个字符串,问能不能在该串的前中后部找到相同的子串,输出最长的字串的长度. 分析:km ...
- HDU 4763 Theme Section(KMP+枚举公共前后缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...
- HDU 4763 Theme Section ( KMP next函数应用 )
设串为str, 串长为len. 对整个串求一遍next函数,从串结尾开始顺着next函数往前找<=len/3的最长串,假设串长为ans,由于next的性质,所以找到的串肯定满足E……E这种形式, ...
- HDU - 4763 Theme Section (KMP的next数组的应用)
给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...
- hdu 4763 Theme Section(next数组找串中三段相等)
题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...
随机推荐
- 32-1题:不分行从上到下打印二叉树/BFS/deque/queue
题目 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 考点 1.广度优先遍历 2.binary tree 3.queue 4.deque 思路 按层打印:8.6.10.5.7.9.11 用ST ...
- SummerVocation_Learning--java的String类方法总结
壹: public char charAt(int index),返回字符串中第index个字符. public int length(), 返回字符串长度. public int indexOf(S ...
- 题解 P3367 【【模板】并查集】
#include<iostream> #include<cstdio> using namespace std; int n,m,x,y,z; ]; //f[i]表示i的祖先 ...
- 二十二、MySQL 正则表达式
MySQL 正则表达式 在前面的章节我们已经了解到MySQL可以通过 LIKE ...% 来进行模糊匹配. MySQL 同样也支持其他正则表达式的匹配, MySQL中使用 REGEXP 操作符来进行正 ...
- HashMap 排序
本文章,摘抄自:2018黑马程序最新面试题汇总 已知一个 HashMap<Integer,User>集合, User 有 name(String)和 age(int)属性.请写一个方法实现 ...
- vue 项目中使用mock假数据实现前后端分离
也是查了很多的资料,整理出来.实现了前后端的分离,用到的技术vue-cli,webpack,node,json-server.首先全局安装json-server cnpm i json-server ...
- 高并发架构系列:如何从0到1设计一个类Dubbo的RPC框架
在过去持续分享的几十期阿里Java面试题中,几乎每次都会问到Dubbo相关问题,比如:“如何从0到1设计一个Dubbo的RPC框架”,这个问题主要考察以下几个方面: 你对RPC框架的底层原理掌握程度. ...
- callback回调函数【转】
请给作者点赞--> 原文链接 什么是回调函数? 我们绕点远路来回答这个问题. 编程分为两类:系统编程(system programming)和应用编程(application programmi ...
- python资源大全2
原文链接 网络 Scapy, Scapy3k: 发送,嗅探,分析和伪造网络数据包.可用作交互式包处理程序或单独作为一个库. pypcap, Pcapy, pylibpcap: 几个不同 libpcap ...
- leetcode 【 Unique Paths 】python 实现
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...