POJ1059Glass Beads
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
Output
Sample Input
4
helloworld
amandamanda
dontcallmebfu
aaabaaa
Sample Output
10
11
6
5
题意:
给一个字符串(环),求出其最小表示的头位置下标。比如aaaba(pos)aa中第5个a为头。
方法:
1,最小表示法。
2,后缀自动机。
后缀自动机求最小表示:
把字符串S复制SS,然后构建后缀自动机。因为后缀自动机得到的是任意区间的字符串[i,j] 。然后跟着root一直找可以取到的最小即可。长度为|S|。
(ps:这里可以类比字典树trie树的排序功能,可以看一下)。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<memory>
using namespace std;
const int maxn=;
char chr[maxn];int L;
struct SAM
{
int slink[maxn],tran[maxn][],maxlen[maxn],root,sz,Last;
void init()
{
root=;sz=;Last=;maxlen[]=slink[]=;
memset(tran[],,sizeof(tran[]));
}
void add(int x)
{
int np=++sz,p=Last;Last=np;
maxlen[np]=maxlen[p]+;
memset(tran[np],,sizeof(tran[np]));
while(p&&!tran[p][x]) tran[p][x]=np,p=slink[p];
if(!p) slink[np]=;
else {
int q=tran[p][x];
if(maxlen[q]==maxlen[p]+) slink[np]=q;
else {
int nq=++sz;
memcpy(tran[nq],tran[q],sizeof(tran[q]));
slink[nq]=slink[q],slink[np]=slink[q]=nq;
maxlen[nq]=maxlen[p]+;
while(p&&tran[p][x]==q) tran[p][x]=nq,p=slink[p];
}
}
}
void solve()
{
int Now=root;
for(int i=;i<L;i++){
for(int j=;j<;j++){
if(tran[Now][j]) {
Now=tran[Now][j];break;
}
}
}
printf("%d\n",maxlen[Now]-L+);
}
};
SAM S;
int main()
{
int i,j,n;
while(~scanf("%d",&n)){
while(n--){
S.init();
scanf("%s",chr);
L=strlen(chr);
for(i=;i<L;i++) S.add(chr[i]-'a');
for(i=;i<L;i++) S.add(chr[i]-'a');
S.solve();
}
}
return ;
}
POJ1059Glass Beads的更多相关文章
- HDU 1817Necklace of Beads(置换+Polya计数)
Necklace of Beads Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 【USACO】beads
题目: You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, othe ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...
- 数学计数原理(Pólya):POJ 1286 Necklace of Beads
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7763 Accepted: 3247 ...
- poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)
Description Beads of red, blue or green colors are connected together into a circular necklace of ...
- POJ 1286 Necklace of Beads(项链的珠子)
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7874 Accepted: 3290 ...
- Necklace of Beads(polya计数)
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7451 Accepted: 3102 ...
- 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 ...
随机推荐
- PJzhang:shell基础入门的2个疗程-two
猫宁!!! 第6节:重定向 标准输入,标准输出,错误输出 输入重定向符号'<' 输出重定向符号'>'(清空之后再输入),'>>'(当前内容不变,在最后一行追加),'2>' ...
- PJzhang:微软出口管制条例
猫宁!!! 翻译:PJzhang 微软公司的技术,产品和服务受美国出口管理法规(EAR)的约束,由美国商务部工业与安全局(BIS)监督执行. 此外,财政部,国务院对其他与出口相关的技术和产品转让,交易 ...
- MessageBox显示位置
假设存在2个窗口类CImDlg与CChatDlg,如果希望MessageBox跟随CChatDlg,方法是 CChatDlg *pDlg = xxx; pDlg->MessageBox();
- pikachu-SQL注入
参考网址: http://www.mamicode.com/info-detail-2795438.html
- 批量删除Maven本地仓库中未下载完成的jar包(不完整的jar包)
1.删除repository库目录下所有后缀名是.lastUpdated的文件 2.进入maven本地仓库地址: CMD进入windows的路径(或在仓库目录的地址栏直接输入CMD,回车自动打开); ...
- typescript中类型断言好理解也好用
类型断言是个好用的玩意,虽然typescript很强大,但是有时还不如我们知道一个值的类型,导致在开发过程中总是报一些令人头痛的类型错误.使用断言,简单来说就是先做好一个假设,使得编译通过. 我一开始 ...
- C++学习 之 继承(笔记)
1.继承基础: 继承就像是生物里的遗传与变异,即派生类(子代)从基类(父代)那里继承基类的某些特性(遗传),并在此基础上拥有自己的特性(变异). C++派生语法: class Base//定义一个基类 ...
- C++练习 | 单链表的创建与输出(结构体格式)
#include <iostream> #include <stdio.h> using namespace std; #define OK 1 #define ERROR 0 ...
- MySQL数据库增删改查SQL语句(2018整理集合大全)
查看数据库 show databases; 使用数据库 use 数据库名; 创建数据库 CREATE DATABASE 数据库名; 删除数据库 DROP DATABASE 数据库名; 创建表 cre ...
- Codeforces 1190B. Tokitsukaze, CSL and Stone Game
传送门 不妨把每一堆按照石头数量从小到大排序 注意到每次只能拿一个石头,那么不论何时每堆石头的排名都是一样的 那么最终所有堆的状态一定就是 $0,1,2,...,n-1$,现在每一堆最终的石头数量都确 ...