zoj 2006 Glass Beads
Glass Beadshttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1006
Time Limit: 2 Seconds Memory Limit: 65536 KB
Once upon a time there was a famous actress. As you may expect, she played mostly Antique Comedies most of all. All the people loved her. But she was not interested in the crowds. Her big hobby were beads of any kind. Many bead makers were working for her and they manufactured new necklaces and bracelets every day. One day she called her main Inspector of Bead Makers (IBM) and told him she wanted a very long and special necklace.
The necklace should be made of glass beads of different sizes connected to each other but without any thread running through the beads, so that means the beads can be disconnected at any point. The actress chose the succession of beads she wants to have and the IBM promised to make the necklace. But then he realized a problem. The joint between two neighbouring beads is not very robust so it is possible that the necklace will get torn by its own weight. The situation becomes even worse when the necklace is disjoined. Moreover, the point of disconnection is very important. If there are small beads at the beginning, the possibility of tearing is much higher than if there were large beads. IBM wants to test the robustness of a necklace so he needs a program that will be able to determine the worst possible point of disjoining the beads.
The description of the necklace is a string A = a1a2 ... am specifying sizes of the particular beads, where the last character am is considered to precede character a1 in circular fashion.
The disjoint point i is said to be worse than the disjoint point j if and only if the string aiai+1 ... ana1 ... ai-1 is lexicografically smaller than the string ajaj+1 ... ana1 ... aj-1. String a1a2 ... an is lexicografically smaller than the string b1b2 ... bn if and only if there exists an integer i, i <= n, so that aj=bj, for each j, 1 <= j < i and ai < bi.
Input
The input consists of N cases. The first line of the
input contains only positive integer N. Then follow the cases. Each case
consists of exactly one line containing necklace description. Maximal length of
each description is 10000 characters. Each bead is represented by a lower-case
character of the english alphabet (a--z), where a < b ... z.
Output
For each case, print exactly one line containing
only one integer -- number of the bead which is the first at the worst possible
disjoining, i.e. such i, that the string A[i] is lexicographically smallest
among all the n possible disjoinings of a necklace. If there are more than one
solution, print the one with the lowest i.
Sample
Input
4
helloworld
amandamanda
dontcallmebfu
aaabaaa
Sample Output
10
11
6
5
题意:输出字典序最小的同构字符串首位置
最小表示法
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10001
using namespace std;
char s[N];
int len;
int main()
{
int n,i,j,k;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
len=strlen(s);
i=; j=;
while(i<len&&j<len)
{
k=;
while(k<len && s[(i+k)%len]==s[(j+k)%len]) k++;
if(k==len) break;
if(s[(i+k)%len]<s[(j+k)%len]) j=max(j+k+,i+);
else i=max(i+k+,j+);
}
printf("%d\n",min(i,j)+);
}
}
zoj 2006 Glass Beads的更多相关文章
- POJ1509 Glass Beads
Glass Beads Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 4314 Accepted: 2448 Descr ...
- POJ1509 Glass Beads(最小表示法 后缀自动机)
Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 4901 Accepted: 2765 Description Once ...
- 【POJ1509】Glass Beads
[POJ1509]Glass Beads [题目描述]给定字符串S,并规定首尾相连成环,求出最小字典序. [输入]输入有多个数据,第一行只包括正整数N,表示有N组数据.每个数据包括一行,输入该字符串. ...
- cogs 2123. [HZOI 2015] Glass Beads
2123. [HZOI 2015] Glass Beads ★★★ 输入文件:MinRepresentations.in 输出文件:MinRepresentations.out 简单对比时 ...
- UVALive 5545 Glass Beads
Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...
- POJ 1509 Glass Beads
Description 求字符串的最小循环表示. Sol SAM. 把原串复制一遍,建出SAM,然后每次选最小的一个跑 \(len\) 次,这就是最小循环表示的最后一个节点,然后 \(x-len+1\ ...
- 1509 -- Glass Beads POJ
题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...
- 杂项(最小表示法):HZOI 2015 Glass Beads
[题目描述] 给定长度为n(n<=300000)的循环同构的字符串,定义最小表示为该字符串的字典序最小的同构表示,请输出这个表示. [输入格式] 第一行是串的长度,第二行是字符串. [输出格式] ...
- UVA 719 / POJ 1509 Glass Beads (最小表示法/后缀自动机)
题目大意: 给出一个长度为N的字符串,求其字典序最小的循环同构. N<=10W. 算法讨论: 算法一.最小表示法.定义题. 算法二.后缀自动机. Codes: #include <iost ...
随机推荐
- codeforces 319B Psychos in a Line(模拟)
There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At eac ...
- php+原生ajax实现图片文件上传功能实例
html+js 代码 <!DOCTYPE html> <html> <head> <title>Html5 Ajax 上传文件</title> ...
- Nodejs中关于模块的总结
关于Nodejs中的模块 概念 Nodejs在ECMAScript的基础上扩展并封装了许多高级特性,如文件访问.网络访问等,使得Nodejs成为一个很好的Web开发平台.基于Nodejs这个平台将We ...
- 在线求助man page
一.举例——输入“man date” 图1 图2 图3 二.man之概述 用于:命令的使用说明 用法:man 命令 man page:执行“man 命令”后,出现的屏幕界面 补:man是manual( ...
- ChromeSwitchySharp代理设置步骤
步骤: 1.新增情景模式配置如下: 2.设置切换规则 3.先“直接连接”进行登录:然后切换到“自动切换模式”访问对应url
- Java中的死锁问题
死锁问题: 例如有两个线程, 线程1与线程2. 线程1在执行的过程中, 要锁定对象1, 2才能完成整个操作, 首先锁定对象1, 再锁定对象2. 线程2在执行的过程中, 要锁定对象2, 1才能完成整个操 ...
- SPDY以及HTTP2.0
背景介绍 HTTP2.0跟SPDY在不少理念上是相似的,目的都是为了提升HTTP1.1的性能. HTTP2.0将会是业界的标准,比SPDY要完善,今后可能会都转向http2.0而放弃SPDY. SPD ...
- 2019 front end jobs collection
2019 front end jobs collection Alibaba https://ant.design/docs/spec/work-with-us-cn https://www.yuqu ...
- linux路由表的配置
linux路由表的配置 一.原理说明 1.路由表(table)从0到255进行编号,每个编号可以对应一个别名,编号和别名的对应关系在linux下放在/etc/iproute2/rt_tables这个文 ...
- Delphi 的绘图功能[8] - TextOut、TextWidth、TextHeight
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...