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顺时针绕成一个圈,每次取一个做开头字母并 ...
随机推荐
- 服务器安全策略之《通过IP安全策略阻止某个IP访问的设置方法》
现在我们在布署好了一个网站,发布到外网后就意味着将会接受来自四面八方的黑客攻击,这个情况很常见,我们的网站基本上每天都要接受成千上万次的攻击,有SQL注入的.有代码注入的.有CC攻击等等...而我作为 ...
- 《剑指offer》面试题45 圆圈中最后剩下的数字(Java版本)
引言 这道题网上的讲解都是直接抄书,没意思,所以想自己写一写,补充一下,便于自己理解.另外,大家都忽略了经典解法,虽然这种解法效率不及第二种,但是我觉得在项目当中阅读性其实很重要,牺牲一点点效率保证代 ...
- 两个button之间的间距如何去掉
在外层加上一个父元素,设置font-size:0;即可. 两个按钮之间的间距如何去掉
- [LeetCode] 398. Random Pick Index ☆☆☆
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 2015/11/6用Python写游戏,pygame入门(6):控制大量的对象
昨天我们已经实现了这个游戏的三个基本类. 但是现在它还是没办法做成一个适合玩的游戏,毕竟只有一架敌机的游戏是很乏味的.所以,我们需要好多子弹,也需要好多敌机. 所以,我们要创建list,这个list存 ...
- What Does “Neurons that Fire Together Wire Together” Mean?
What Does “Neurons that Fire Together Wire Together” Mean? I’ve heard the phrase “neurons that fire ...
- 【leetcode 简单】 第七十三题 丑数
编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 输出: true ...
- 2016.6.26——Maximum Depth of Binary Tree
Maximum Depth of Binary Tree 本题收获 1.树时使用递归 2.注意边界条件时输出的值,仔细阅读题意,若是面试时,问清边界条件. 题目: Given a binary tre ...
- 20155303 2016-2017-2 《Java程序设计》第八周学习总结
20155303 2016-2017-2 <Java程序设计>第八周学习总结 目录 学习内容总结(Linux命令) 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周考 ...
- SQLAlchemy-对象关系教程ORM-一对多(外键),一对一,多对多
一:一对多 表示一对多的关系时,在子表类中通过 foreign key (外键)引用父表类,然后,在父表类中通过 relationship() 方法来引用子表的类. 在一对多的关系中建立双向的关系,这 ...