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 ...
随机推荐
- python基础知识(元组)
元组 不能更改内容 元组 (元素1,元素2) 元组的创建和删除 使用赋值运算符直接创建元组 元组名 = (元素1,元素2........) 只创建一个元素的元组 元组名 = (元素1,) 创建空 ...
- 云计算共享组件--消息队列rabbitmq(3)
一.MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们. 消息传 ...
- TensorFlow实战第八课(卷积神经网络CNN)
首先我们来简单的了解一下什么是卷积神经网路(Convolutional Neural Network) 卷积神经网络是近些年逐步兴起的一种人工神经网络结构, 因为利用卷积神经网络在图像和语音识别方面能 ...
- 贡献python prim多源最短路搜索算法 numba加速方法的demo和总结
1.测试两个算法 #coding:utf-8 import time import numba import numpy as np ''' 使用numba加速总结, (1).在数值计算比如int f ...
- DDOS常见攻击类型和防御措施
DDOS 攻击类型: SYN Flood 攻击 ACK Flood 攻击 UDP Flood 攻击 ICMP Flood 攻击 Connection Flood 攻击 HTTP Get 攻击 UDP ...
- HDU 1260 Tickets (动态规划)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- Spring IOC 和Aspectj AOP
1.Aspectj AOP 是一套独立的AOP 解决方案,不仅限于java应用,不依赖其他方案,属于编译时增强,有自己单独的编译器.Spring AOP 是基于Spring 容器的的AOP解决方式,属 ...
- Ubuntu基本操作(博主想上传图片给服务器的一些命令)
1.将当前目录下的文件移动至指定文件夹,这里用移动至网站的根目录做示范 sudo mv bamboo.jpg /val/www/html mv bamboo.jpg /val/www/html 2.进 ...
- python_0基础开始_day08
第八节 1,文件操作 文件操作目的: 持久化,永久存储 (数据库之前 -- 文件操作就是代替数据库) 读 1,找到文件位 2,双击打开 3,进行一些操作 4,关闭文件 open() 打开,通过pyth ...
- Codeforces 1201E2. Knightmare (hard)
传送门 看到棋盘先黑白染色冷静一下 然后分析发现,如果初始时两只马在同色的格子,那么一定是后手吃先手 反之一定是先手吃后手 所以分类讨论一下,如果初始在同色的格子,并且后手到达终点的步数更少,那么后手 ...