USACO 5.5 Hidden Password
Hidden Password
ACM South Eastern Europe -- 2003
Sometimes the programmers have very strange ways of hiding their passwords. Billy "Hacker" Geits chooses a string S composed of L (5 <= L <= 100,000) lowercase letters ('a'..'z') with length L. Then he makes and sorts all L-1 one-letter left cyclic shifts of the string. He then takes as a password one prefix of the lexicographically first of the obtained strings (including S).
For example consider the string "alabala". The sorted cyclic one-letter left shifts (including the initial string) are:
aalabal
abalaal
alaalab
alabala
balaala
laalaba
labalaa
Lexicographically, first string is 'aalabal'. The first letter of this string ('a') is the 'a' that was in position 6 in the initial string (counting the first letter in the string as position 0).
Write a program that, for given string S, finds the start position of the first letter of the sorted list of cyclic shifts of the string. If the first element appears more than once in the sorted list, then the program should output the smallest possible initial position.
PROGRAM NAME: hidden
INPUT FORMAT
- Line 1: A single integer: L
- Line 2..?: All L characters of the the string S, broken across lines such that each line has 72 characters except the last one, which might have fewer.
SAMPLE INPUT (file hidden.in)
7
alabala
OUTPUT FORMAT
- Line 1: A single integer that is the start position of the first letter, as described above.
SAMPLE OUTPUT (file hidden.out)
6 ————————————————————————————————————————————————————————题解
它只让你输出最小值你为什么要排序sjq你是sb吗!!!!
这道题一开始的思路是建l个string排个序……【为什么要排序!!!】然后爆空间了
然后开始用lcp(最小公共前缀)然后排个序【为什么要排序!!!】然后超时了
我想,不能超时啊……nlognlogn啊……【为什么要排序!!!】
我又一想,哎,我为什么要排序啊?【对啊!!!!】
然后我做完了
好了这道题的思路就是把字符串复制一下,使它变成2*l长,这样我们断环为链,再处理成一个字符串hash
对于每两个开头a,b二分得出他们的lcp,然后比较lcp的下一位,或者他们完全相等比较a,b大小
这道题我们只输出最小值O(L)就可以选出最小值,加上二分的复杂度,最大也就是O(LlogL) 【所以你为什么要排序!!!】
/*
ID: ivorysi
LANG: C++
PROG: hidden
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#include <stack>
#include <map>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x5f5f5f5f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
char str[],t[];
int l,e[],ha[],x,cnt,c;
bool check(int a,int b,int len) {
return ((ll)e[*l-a-len]*(ha[a+len-]-ha[a-])%mo - (ll)e[*l-b-len]*(ha[b+len-]-ha[b-])%mo)%mo==;
}
int binary(int a,int b) {
int le=,ri=l,mid;
while(le<ri) {
mid=(le+ri+)>>;//因为最后返回的是le
if(check(a,b,mid)) le=mid;
else ri=mid-;
}
return le;
}
bool cmp(const int &a,const int &b) {
x=binary(a,b);
return x==l ? a<b : str[a+x]<str[b+x];
}
void solve() {
scanf("%d",&l);
siji(i,,(l-)/+) {
scanf("%s",str++(i-)*);
}
strcpy(t+,str+);
strcat(str+,t+);
e[]=;
siji(i,,*l) {
e[i]=(ll)e[i-]*base%mo;
}
siji(i,,*l) {
ha[i]=(ll)(ha[i-]+(ll)e[i]*(str[i]-'a'+)%mo)%mo;
}
c=;
siji(i,,l) {
if(cmp(i,c)) c=i;
}
printf("%d\n",c-);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("hidden.in","r",stdin);
freopen("hidden.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 5.5 Hidden Password的更多相关文章
- USACO 5.5 Hidden Password(搜索+优化)
水了好几下. 优化1:开始初始化的时候,如果左边那个也是最小值,那么此点就不用进队了. 优化2:如果队列里的位置,已经超过了后面位置的初始位置,那么后面这个位置也没用了. /* ID: cuizhe ...
- [洛谷P1709] [USACO5.5]隐藏口令Hidden Password
洛谷题目链接:[USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N< ...
- P1709 [USACO5.5]隐藏口令Hidden Password
P1709 [USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<= ...
- 洛谷 P1709 [USACO5.5]隐藏口令Hidden Password
P1709 [USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<= ...
- [USACO5.5]隐藏口令Hidden Password [最小表示法模板]
最小表示法就是一个字符串构成一个环,找以哪个点为开头字典序最小. 然后我们就可以用n2的算法愉快的做啦~实际上有O(n)的做法的,就是用两个指针扫,如果这两个位置的字典序相等,就一起往后,如果某一个大 ...
- toj 3019 Hidden Password (最小表示法)
Hidden Password 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 53 测试通过: 19 描述 Some time the progr ...
- zoj 1729 Hidden Password
Hidden Passwordhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=729 Time Limit: 2 Seconds ...
- HTML——form表单中常用标签 form input (text hidden password radio checkbox reset submit ) select(option)总结
<form action="" method="get"> <!-- placeholder="请输入文本" 显示提示 r ...
- 洛谷P1709 [USACO5.5]隐藏口令Hidden Password(最小表示法)
题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并 ...
随机推荐
- 安装SQL Sever数据库失败的解决办法
视频链接:https://www.bilibili.com/video/av12651739/ 我安装了SQL Sever2014.遇到了好多好多问题啊,整的我都快疯了.大致遇到的问题和解决办法如下. ...
- bzoj千题计划170:bzoj1968: [Ahoi2005]COMMON 约数研究
http://www.lydsy.com/JudgeOnline/problem.php?id=1968 换个角度 一个数可以成为几个数的约数 #include<cstdio> #incl ...
- JavaScript 运行机制之执行顺序详解
JavaScript是一种描述型脚本语言,它不同于 Java 或 C# 等编译性语言,它不需要进行编译成中间语言,而是由浏览器进行动态地解析与执行.如果你不能理解 JavaScript 语言的运行机制 ...
- 知名网站内部资料:WEB页面内容优化管理与性能技巧
回想一下,以前我们不得不花费大量时间去优化页面内容(图片.CSS等等),如今用户有更快速的互联网链接,我们似乎能够使用更大的图像或更大的闪 存文件,里面包含的有视频或者图片.然而,随着移动开发的兴起, ...
- debian旧日笔记
18:45 2007-10-7 序 图形界面较好的Linux操作系统有三个主要的发行版本:RedHat, Debian, SuSE.自RedHat9后,RedHat以Fedora为名发行新的版本.仍然 ...
- 用threading和Queue模块实现多线程的端口扫描器
一.Queue模块基础 q = Queue.Queue() q.qsize() 返回队列的大小 q.empty() 如果队列为空,返回True,反之Fals ...
- React Native新手入门
前言 React Native是最近非常火的一个话题,想要学习如何使用它,首先就要知道它是什么. 好像面对一个新手全面介绍它的文章还不多,我就归纳一下所有的资料和刚入门的小伙伴一起来认识它~ 将从以下 ...
- 最小生成树 kuangbin专题最后一个题
题目链接:https://cn.vjudge.net/contest/66965#problem/N 注释:这道题需要用krustra,用prim的话可能会超时.并且在计算距离的时候要尽量减少步骤,具 ...
- 南京邮电大学 CTF 逆向部分 Writeup
Hello,RE! 提示 IDA 中按 R . Google 到 IDA 中 R 快捷键是 Character ,转为字符串. 丢进 IDA(虽然我并不会使用 IDA 有个 strcmp 函数,比较 ...
- 在Linode VPS上搭建最新版Transmission
在Linode VPS上搭建最新版Transmission 2015-09-16 by Hansen 原文链接:http://www.hansendong.me/archives/124.html 以 ...