【BZOJ 1031】[JSOI2007]字符加密Cipher(后缀数组模板)
【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1031
【题意】
【题解】
后缀数组模板题;
把整个字符串扩大一倍.
即长度乘2
然后搞出后缀数组;
然后顺序枚举i;
对于sa[i]< n的输出对应的s[sa[i]+n-1]就好了
后缀的含义是把后缀按照字典序从小到大排一下.
按照这个规则;
就能搞了;
必然是在前n个字符中就能比较出来;
所以及时后缀多了一些也没事.
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5+100;
char s[N * 2];
int sa[N * 2], wv[2][350005], tong[N * 2];
bool cmp(int *tmp, int x, int y, int j)
{
return tmp[x] == tmp[y] && tmp[x + j] == tmp[y + j];
}
void getsa(int n, int m)
{
int p = 0, *x = wv[0], *y = wv[1];
rep1(i, 0, n - 1)
++tong[x[i] = s[i]];
rep1(i, 1, m - 1)
tong[i] += tong[i - 1];
rep2(i, n - 1, 0)
sa[--tong[x[i]]] = i;
for (int j = 1; p != n; j <<= 1, m = p)
{
p = 0;
rep1(i,n-j,n-1)
y[p++] = i;
rep1(i, 0, n - 1)
if (sa[i] >= j)
y[p++] = sa[i] - j;
rep1(i, 0, m - 1)
tong[i] = 0;
rep1(i, 0, n - 1)
++tong[x[y[i]]];
rep1(i, 1, m - 1)
tong[i] += tong[i - 1];
rep2(i, n - 1, 0)
sa[--tong[x[y[i]]]] = y[i];
swap(x, y), p = 1, x[sa[0]] = 0;
rep1(i, 1, n - 1)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
}
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
int n;
scanf("%s", s);
n = strlen(s);
rep1(i, 0, n - 1)
s[i + n] = s[i];
getsa(n << 1 | 1, 128);
rep1(i, 1, n << 1)
if (sa[i] < n)
putchar(s[sa[i] + n - 1]);
puts("");
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【BZOJ 1031】[JSOI2007]字符加密Cipher(后缀数组模板)的更多相关文章
- BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题
BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题 将字符串复制一遍接在原串后面,然后后缀排序即可. #include <cmath> #include &l ...
- bzoj 1031 [JSOI2007]字符加密Cipher 后缀数组模板
题目链接 题目描述 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作: ...
- BZOJ 1031 [JSOI2007]字符加密Cipher 后缀数组教程
1031: [JSOI2007]字符加密Cipher Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一 ...
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6014 Solved: 2503[Submit ...
- bzoj 1031: [JSOI2007]字符加密Cipher 後綴數組模板題
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3157 Solved: 1233[Submit ...
- 【BZOJ1031】[JSOI2007]字符加密Cipher 后缀数组
[BZOJ1031][JSOI2007]字符加密Cipher Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的 ...
- 1031. [JSOI2007]字符加密【后缀数组】
Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作 ...
- bzoj 1031 [JSOI2007]字符加密Cipher
求出来后缀数组的rank就行了,不会可以去看集训队论文. #include<iostream> #include<cstdio> #include<cstring> ...
- bzoj 1031: [JSOI2007]字符加密Cipher【后缀数组】
算是SA的裸题了 把串复制一遍接在原串后面,然后求SA,然后按着SA的顺序输出尾字符即可 #include<iostream> #include<cstdio> #includ ...
随机推荐
- C#中对XML的操作
现在有一个xml文件,名称:BookStore.xml,数据如下: <?xml version="1.0" encoding="gb2312"?>& ...
- TabControl里面添加From
private void dynamicDll() { string dllName = "dll"; Assembly ass = Assembly.Load(dllName); ...
- 【习题 6-2 UVA - 712】S-Trees
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] dfs模拟一下就好. 先预处理一个dfs. 搞出来x叶子节点它的值是什么 [代码] /* 1.Shoud it use long l ...
- 洛谷——P1601 A+B Problem(高精)
https://www.luogu.org/problem/show?pid=1601#sub 题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑 ...
- mysql 表的timestamp为自动添加
新设计表时可以执行语句: `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP C ...
- CloudFoundry hm9000原理及排错
hm9000跟hm_next(healthmanager)功能类似.在cloudfoundry集群中担任至关重要的角色 - 尝试启动缺失情况下的实例,停止异常实例 - 获知和报告应用执行的实际实例个数 ...
- 【例题5-7 UVA - 136】Ugly Numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个丑数x,都能生成3个丑数2x,3x,5x 则我们以1作为起点. 生成丑数. 每次取出set里面最小的那个数. 然后用它去生成其他 ...
- sublime text2 基本配置及结合Python 环境
参考: http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html http://www.zhihu.com/question/ ...
- css 单行图片文字水平垂直居中汇总
(1) 水平居中 a. 行内元素水平居中 因为img是行内元素(行内块级元素也一样)父级元素设置text-align:center即可,例如: <div style="width: 6 ...
- Python 标准库 —— glob
glob库是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用 windows 下的文件搜索差不多.查找文件只用到三个匹配符: "*", 匹配 0 个或多个 ...