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的更多相关文章

  1. USACO 5.5 Hidden Password(搜索+优化)

    水了好几下. 优化1:开始初始化的时候,如果左边那个也是最小值,那么此点就不用进队了. 优化2:如果队列里的位置,已经超过了后面位置的初始位置,那么后面这个位置也没用了. /* ID: cuizhe ...

  2. [洛谷P1709] [USACO5.5]隐藏口令Hidden Password

    洛谷题目链接:[USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N< ...

  3. P1709 [USACO5.5]隐藏口令Hidden Password

    P1709 [USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<= ...

  4. 洛谷 P1709 [USACO5.5]隐藏口令Hidden Password

    P1709 [USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<= ...

  5. [USACO5.5]隐藏口令Hidden Password [最小表示法模板]

    最小表示法就是一个字符串构成一个环,找以哪个点为开头字典序最小. 然后我们就可以用n2的算法愉快的做啦~实际上有O(n)的做法的,就是用两个指针扫,如果这两个位置的字典序相等,就一起往后,如果某一个大 ...

  6. toj 3019 Hidden Password (最小表示法)

    Hidden Password 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 53 测试通过: 19 描述 Some time the progr ...

  7. zoj 1729 Hidden Password

    Hidden Passwordhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=729 Time Limit: 2 Seconds ...

  8. HTML——form表单中常用标签 form input (text hidden password radio checkbox reset submit ) select(option)总结

    <form action="" method="get"> <!-- placeholder="请输入文本" 显示提示 r ...

  9. 洛谷P1709 [USACO5.5]隐藏口令Hidden Password(最小表示法)

    题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并 ...

随机推荐

  1. 即时通信系统Openfire分析之二:主干程序分析

    引言 宇宙大爆炸,于是开始了万物生衍,从一个连人渣都还没有的时代,一步步进化到如今的花花世界. 然而沧海桑田,一百多亿年过去了…. 好复杂,但程序就简单多了,main()函数运行,敲个回车,一行Hel ...

  2. PHP7 学习笔记(二)PHP5.9 升级到PHP7 遇到的一些坑的记录(php-fpm 图解)

    apache_event_php-fpm 示意图: nginx-php-fpm示意图: Worker-Master-Server TCP-Nginx_PHP Nginx-FastCGI 1.使用$_G ...

  3. 【翻译】Voidbox: Docker on YARN

    原文链接:Voidbox – Docker on YARN 读了此文,收获良多,翻译之,方便以后查看~ 文章介绍了Hulu北京大数据团队开发的Docker On YARN实现:Voidbox,一种基于 ...

  4. 二、Kafka基础实战:消费者和生产者实例

    一.Kafka消费者编程模型 1.分区消费模型 分区消费伪代码描述 main() 获取分区的size for index =0 to size create thread(or process) co ...

  5. 一、Kafka初认识

    一.kafka使用背景 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候,是否会遇到这样的一些问题: 我们想分析下用户行为(pageviews),以便我们设计出更好的广告位 我 ...

  6. 在嵌入式Linux系统(OK6410)中移植Boa 服务器

    OK6410的Boa服务器移植: <一> Boa的编译 1. 从 www.boa.org 下载 Boa 服务器的最新版:boa-0.94.13.tar.gz. 2. 解压:tar xzf ...

  7. 对web标准的理解,以及对w3c组织的认识

    (1)web标准规范要求,书写标签必须闭合.标签小写.不乱嵌套,可提高搜索机器人对网页内容的搜索几率.--- SEO(2)建议使用外链css和js脚本,从而达到结构与行为.结构与表现的分离,提高页面的 ...

  8. Wannacry样本取证特征与清除

    一.取证特征 1)网络域名特征 http://www.iuqerfsodp9ifjaposdfjhgosurijfaewrwergwea.com 2)文件特征 母体文件 mssecsvc.exe c: ...

  9. C# 序列化高级用法

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  10. 【转】WCF光芒下的Web Service

    WCF光芒下的Web Service 学习.NET的开发人员,在WCF的光芒照耀下,Web Service 似乎快要被人遗忘了.因为身边做技术的人一开口就是WCF多么的牛逼!废话不多,本人很久不写博客 ...