POJ 1509 Glass Beads 后缀自动机
2016-04-14 20:47
323 查看
求给定字符串s从哪个位置开始的循环同构串字典序最小。
如果复制一遍s,发现其所有循环同构都是新字符串ss的长度为|s|的子串。
于是后缀自动机。。dfs,不断地走字典序最小的边,直到走了|s|次即可。
而且发现,不管怎么走,SAM都至少能走|s|次。因此一条路往黑里走即可。
然后我们需要知道状态的Right集合的最小值。
而我们知道,SAM主链上的状态(由last连接的),max值均为Right集合的最小值,那走|s|次的状态可不可能不在主链上呢(即为被拆出来的nq点)?答案是不可能的。新拆出来的点的max值均不超过|s|。
新拆出来的点,其出边为非连续转移,由x+c+y,x可以是主链上也可以不在主链上,而y必定在主链上。由于对于连续转移,没有支链会比主链长………………………………………………………………………………………………………………………………………………………………
Total Submissions: 3629 Accepted: 2058
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
如果复制一遍s,发现其所有循环同构都是新字符串ss的长度为|s|的子串。
于是后缀自动机。。dfs,不断地走字典序最小的边,直到走了|s|次即可。
而且发现,不管怎么走,SAM都至少能走|s|次。因此一条路往黑里走即可。
然后我们需要知道状态的Right集合的最小值。
而我们知道,SAM主链上的状态(由last连接的),max值均为Right集合的最小值,那走|s|次的状态可不可能不在主链上呢(即为被拆出来的nq点)?答案是不可能的。新拆出来的点的max值均不超过|s|。
新拆出来的点,其出边为非连续转移,由x+c+y,x可以是主链上也可以不在主链上,而y必定在主链上。由于对于连续转移,没有支链会比主链长………………………………………………………………………………………………………………………………………………………………
#include <cstdio> #include <cstring> const int rt = 1, N = 40005; int ch [26], fa , ma ; char s ; int last, cnt, len; void add(char c) { int np = ++cnt, p = last; last = np; ma[np] = ++len; memset(ch[np], 0, sizeof ch[np]); while (p && !ch[p][c]) ch[p][c] = np, p = fa[p]; if (!p) fa[np] = rt; else { int q = ch[p][c]; if (ma[q] == ma[p] + 1) fa[np] = q; else { int nq = ++cnt; ma[nq] = ma[p] + 1; memcpy(ch[nq], ch[q], sizeof ch[q]); fa[nq] = fa[q]; fa[q] = fa[np] = nq; while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p]; } } } void construct(char *s) { int i, l = strlen(s); last = cnt = 1; len = 0; memset(ch[rt], 0, sizeof ch[rt]); for (i = 0; i < l * 2; ++i) add(s[i % l] - 'a'); } int main() { int t, p, l, j; char *r; scanf("%d", &t); while (t--) { scanf("%s", s); construct(s); for (p = rt, l = 0, r = s; *r; ++r) for (j = 0; j < 26; ++j) if (ch[p][j]) { p = ch[p][j]; ++l; break; } printf("%d\n", ma[p] - l + 1); } return 0; }
Glass Beads
Time Limit: 3000MS Memory Limit: 10000KTotal Submissions: 3629 Accepted: 2058
Description
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
Source
Central Europe 1998相关文章推荐
- windows系统下的文件长名和文件短名
- BeanCopier 实现 值传递
- kafka-0.8.1.1总结
- 【android studio】解决android studio drawable新建项目时只有一个drawable目录的问题
- 获取股票历史数据和当前数据的API
- Oracle学习三
- 使用CAShapeLayer与UIBezierPath画出想要的图形
- 虚拟机下网络连接不上
- leetcode24题 题解 翻译 C语言版 Python版
- 在这里开始填一个天坑
- final classes
- Centos 7服务器搭建(二)—安装MySQL
- Java IO1:IO和File
- gitignore添加忽略文件
- 【opencv学习之一】配置QT+opencv环境简单方法
- SDWebImage使用——一个可管理远程图片加载的类库
- iOS webView中JavaScript打开原生ViewController(升级版)
- Android 的上下文菜单: Context Menu,registerForContextMenu(getListView())
- 第3周 C程序语言设计(第4版)-7.2示例分配考生的分数段位人数
- 1.1.2 Linux之前UNIX的历史(2)