Description

求字符串的最小循环表示.

Sol

SAM.

把原串复制一遍,建出SAM,然后每次选最小的一个跑 \(len\) 次,这就是最小循环表示的最后一个节点,然后 \(x-len+1\) 即为答案.

Code

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int N = 20005; int n,rt,lst,cnt,l;
int s[N];
int val[N],par[N],go[N][26]; inline int in(int x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
inline int read(int l=0,char ch=getchar()){ while(ch>'z' || ch<'a') ch=getchar();
while(ch>='a' && ch<='z') s[++l]=ch-'a',ch=getchar();return l; }
void Extend(int w){
int p=lst,np=++cnt;val[np]=val[p]+1,memset(go[np],0,sizeof(go[np]));
while(p && !go[p][w]) go[p][w]=np,p=par[p];
if(!p) par[np]=rt;
else{
int q=go[p][w];
if(val[p]+1 == val[q]) par[np]=q;
else{
int nq=++cnt;
val[nq]=val[p]+1;
memcpy(go[nq],go[q],sizeof(go[q]));
par[nq]=par[q];
par[np]=par[q]=nq;
while(p && go[p][w]==q) go[p][w]=nq,p=par[p];
}
}lst=np;
}
int work(){
int x=rt;
for(int i=1;i<=l;i++)
for(int j=0;j<26;j++) if(go[x][j]){ x=go[x][j];break; }
return val[x]>=l?val[x]-l:val[x];
}
int main(){
n=in();
for(int i=1;i<=n;i++){
l=read(),rt=lst=cnt=1;
val[rt]=par[rt]=0;memset(go[rt],0,sizeof(go[rt]));
for(int i=l+1;i<=2*l;i++) s[i]=s[i-l];
for(int i=1;i<=2*l;i++) Extend(s[i]);
cout<<work()+1<<endl;
}
}

  

POJ 1509 Glass Beads的更多相关文章

  1. ●POJ 1509 Glass Beads

    题链: http://poj.org/problem?id=1509 题解: 给出一个字符串,有一个操作:把首字符放到末尾,形成新的串.求任意次操作后,字典序最小的串的首字母在原串中的位置.(这就是最 ...

  2. POJ 1509 Glass Beads 后缀自动机 模板 字符串的最小表示

    http://poj.org/problem?id=1509 后缀自动机其实就是一个压缩储存空间时间(对节点重复利用)的储存所有一个字符串所有子串的trie树,如果想不起来长什么样子可以百度一下找个图 ...

  3. POJ 1509 Glass Beads【字符串最小表示法】

    题目链接: http://poj.org/problem?id=1509 题意: 求循环字符串的最小表示. 分析: 浅析"最小表示法"思想在字符串循环同构问题中的应用 判断两字符串 ...

  4. UVA 719 / POJ 1509 Glass Beads (最小表示法/后缀自动机)

    题目大意: 给出一个长度为N的字符串,求其字典序最小的循环同构. N<=10W. 算法讨论: 算法一.最小表示法.定义题. 算法二.后缀自动机. Codes: #include <iost ...

  5. 1509 -- Glass Beads POJ

    题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...

  6. PKU 1509 Glass Beads (最小表示法)

    题意:有一个环形字符串,让你找一个位置切一刀使得字符串字母序最小.输出这个位置. 思路:能够看成两个字符串比較.一个是从下标0開始(0~n-1),一个从下标1開始(1~n-1,0). 然后两个指针i= ...

  7. POJ 1509 Glass Beads---最小表示法

    题意: T组数据,每组数据给出一个字符串,求这个字符串的最小表示发(只要求输出起始位置坐标) SAM入门题(检测板子是否正确). 将字符串S加倍丢进SAM中,然后走字符串长度次,每次贪心的沿最小的边走 ...

  8. POJ1509 Glass Beads

    Glass Beads Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 4314   Accepted: 2448 Descr ...

  9. POJ1509 Glass Beads(最小表示法 后缀自动机)

    Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 4901   Accepted: 2765 Description Once ...

随机推荐

  1. Android学习笔记——MixLayout

    该工程的功能是实现LinearLayout+TableLayout 以下代码是MainActivity.java中的代码 package com.example.mixlayout; import a ...

  2. 第1章 认识jQuery

    一.常用的JavaScript库对比 Prototype.Dojo.YUI.Mootools jQuery是一个轻量级的JavaScript库,大型开发必备——由John Resig于2006年创建. ...

  3. ecshop广告-》单张,多张

    //读取广告 function get_ad_id($ad_id){ //读取指定ad_id广告 $sql = 'select * from '. $GLOBALS['ecs']->table( ...

  4. js返回顶部

    1. function scrollTop(){ $(},); } 2. $("#side-bar .gotop").click(function(){ $(},); //返回顶部 ...

  5. How to save milliseconds to DB in NHibernate

    We need to configure Timestamp in Mapping. eg. Map(x => x.ResponseDate).CustomType("Timestam ...

  6. webform中使用webapi,并且使用autofac

    private void AutofacIoCRegister() { HttpConfiguration config = GlobalConfiguration.Configuration; if ...

  7. 【转】Delphi 关键字详解

    absolute //它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同. var Str: string[32]; StrLen: Byte absolute Str; //这个声明 ...

  8. Mac添加bash alias

    1. vim ~/.bash_profile (无则新建) 添加 if [ -f ~/.bashrc ]; then source ~/.bashrcfi 2.vim ~/.bashrc (无则新建) ...

  9. python内置函数每个执行一次

      open    #   with open('log','r') as f:    或者   r=open(filename,r+) with open ('1.txt','r',encoding ...

  10. [工具]json转类

    摘要 这周在园子看到一篇介绍JsonCSharpClassGenerator这个工具的文章,感觉挺实用的,在现在项目中json用的是最多的,所以在转换对应的类的时候,确实挺频繁,所以就研究了一下这个工 ...