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. 一篇很棒的 MySQL 触发器学习教程

    一.触发器概念 触发器(trigger):监视某种情况,并触发某种操作,它是提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动 ...

  2. codevs 1070 普通递归关系

    1070 普通递归关系  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description 考虑以下定义在非负整数n上的递归关系 f( ...

  3. [HTML5和Flash视频播放器]Video.js 学习笔记(一 ) HLS库:videojs-contrib-hls

    DEMO地址:https://github.com/Tinywan/PHP_Experience https://github.com/videojs/videojs-contrib-hls 下载JS ...

  4. [Luogu 3258] JLOI2014 松鼠的新家

    [Luogu 3258] JLOI2014 松鼠的新家 LCA + 树上差分. 我呢,因为是树剖求的 LCA,预处理了 DFN(DFS 序),于是简化成了序列差分. qwq不讲了不讲了,贴代码. #i ...

  5. 2017萧山第5场(2016 Pacific Northwest - Division 1)

    B:Buggy Robot [题意] 一个n*m的地图(1≤n, m≤50),有一个入口和一个出口.给定一个命令序列(上,下,左,右),如果碰到障碍或者边际就忽略.问至少加入或删除多少个的命令,使得能 ...

  6. Java后台获取前端utf-8格式&nbsp;空格,使用trim()消除不了的解决办法

    byte bytes[] = {(byte) 0xC2,(byte) 0xA0}; String UTFSpace = new String(bytes,"utf-8"); htm ...

  7. windows 下 react-native(v0.56) Android 环境搭建踩坑记录

    debugservicereact-native 安装官网 https://reactnative.cn/docs/getting-started.html 根据官网步骤一步步执行下去.还能碰到一些问 ...

  8. Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again错误解决

    rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm  安装了epel源 但  yum -y ...

  9. CSV转excel方法

    步骤一:新建excel文件,数据—>自文本,导入文件 步骤二:选择分隔符,下一步 步骤三:勾选分隔符符合,下一步 步骤四:直接下一步,可在预览里看到格式 步骤五:点击确定,等待数据导入

  10. MUI项目基础框架

    码云SVN仓库地址:https://gitee.com/lim2018/vx/tree/master MUI项目基础框架,底部导航栏切换 目录结构 index为入口页主体,sub1-4为要切换的子页面 ...