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 ...
随机推荐
- 转SQLSERVER 会不会自动加锁
[SQL]提升查询效率与避免LOCK发生nolock: 可能把没有提交事务的数据也显示出来,可能会产生脏读readpast: 会把被锁住的行不显示出来 所有Select加 With (NoLock)解 ...
- C#编程 socket编程之TcpClient,TcpListener,UdpClient
应用程序可以通过 TCPClient.TCPListener 和 UDPClient 类使用传输控制协议 (TCP) 和用户数据文报协议 (UDP) 服务.这些协议类建立在 System.Net.So ...
- 机器学习笔记:支持向量机(svm)
支持向量机(svm)英文为Support Vector Machines 第一次接触支持向量机是2017年在一个在线解密游戏"哈密顿行动"中的一个关卡的二分类问题,用到了台湾教授写 ...
- Flume 概念、模型和特点
Flume Event - Flume 事件 - 被定义为一个具有有效荷载的字节数据流和可选的字符串属性集. Flume Agent- Flume - 代理 - 是一个进程承载从外部源事件流到下一个目 ...
- npm install 报 128 错误
[问题描述] 项目执行npm install的时候特别慢,到最后直接返回错误: verbose exit [ 1, true ] [解决方法] 执行以下两条命令: git config --globa ...
- [Python3] 037 函数式编程 装饰器
目录 函数式编程 之 装饰器 Decrator 1. 引子 2. 简介 3. 使用 函数式编程 之 装饰器 Decrator 1. 引子 >>> def func(): ... pr ...
- 算法 - k-means算法
一.聚类思想 所谓聚类算法是指将一堆没有标签的数据自动划分成几类的方法,属于无监督学习方法,这个方法要保证同一类的数据有相似的特征,如下图所示: 根据样本之间的距离或者说是相似性(亲疏性),把 ...
- python PEP8常用规范(看完你会感谢我的!)
完整的规范移步传送门 pep8规范 官方文档:[https://www.python.org/dev/peps/pep-0008/](https://www.python.org/dev/peps/p ...
- npm操作命令
查看所有高级的npm moudles npm list --depth= 查看所有全局安装的模块 npm list --depth= -global 查找npm全局安装模块路径 npm config ...
- vsCode 前端插件推荐-和插件配置
参考自网站:https://segmentfault.com/a/1190000011779959 插件安装完成之后,还要对一些插件进行配置,例如: vetur默认配置, 配置的过程: 打开 文件 & ...