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]代表的即是最大的相 ...
随机推荐
- git出现误修改如何撤销
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file. 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步, ...
- 项目17-超详细“零”基础kafka入门篇
分类: Linux服务篇,Linux架构篇 1.认识kafka 1.1 kafka简介 Kafka 是一个分布式流媒体平台 kafka官网:http://kafka.apache.org/ (1) ...
- Linux基础-Linux常用命令
Linux(/'lainʌks/)系统特点:稳定,安全,开源(一切皆文件) 装上SSH协议就可以连接Linux 装虚拟机(SSH) win用xshell工具 Linux命令:每日一个linux命令 p ...
- 服务发现与注册-Eureka
1.搭建 创建一个Springboot项目,添加依赖 <dependencies> <!--添加Eureka服务器端依赖--> <dependency> <g ...
- Fakeapp 入门教程(3):参数篇
参数可以让软件自由度更高.Fakeapp的参数并不算多,但是也非常使用.本文就讲解下几个重要的参数.参数设置界面可以通过点击SETTINGS打开. 参数修改无需点击保存,一旦修改直接生效. Proce ...
- Spark架构与作业执行流程简介(scala版)
在讲spark之前,不得不详细介绍一下RDD(Resilient Distributed Dataset),打开RDD的源码,一开始的介绍如此: 字面意思就是弹性分布式数据集,是spark中最基本的数 ...
- linux下安装VMware出错:Gtk-Message: Failed to load module "canberra-gtk-module"解决方法
最近又有兴趣在linux下搭建个虚拟机,于是去找了个VMWorkstation,安装的过程中报了两个错误 Gtk-Message: Failed to load module "pk-gtk ...
- has value '1.8', but '1.7' is required
使用java7,自己又想在空闲时间学一些java8的新特性,故在安装完1.7之后又安装了1.8 eclispe在启动时报’has value ‘1.8’,but’1.7’ is required’的错 ...
- IOS开发---菜鸟学习之路--(十)-实现新闻详细信息浏览页面
前面已经将了上下拉刷新 实现了上下拉刷新后我们的第一级界面就做好,接下来我们就需要实现 新闻详细信息浏览了 我个人认为一般实现新闻详细页面的方法有两种(主要是数据源的不同导致了方法的不同) 第一种是本 ...
- PAT乙级 1001(C)+1054(Java)
准备一天两道题,就这样吧,先从水题开始. 1001.点击查看 分析:看懂题就应该写出来了,注意边界与0情况的处理. #include<stdio.h> #include<math.h ...