hdu5558 Alice's Classified Message
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5558
题目:
Alice's Classified Message
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 789 Accepted Submission(s): 311
S[a…b] means a substring of S ranging from S[a] to S[b] (0≤a≤b<N). If the first i letters have been encrypted, Alice will try to find a magic string P. Assuming P has K letters, P is the longest string which satisfies P=S[T...T+K−1] (0≤T<i,T+K≤N) and P=S[i…i+K−1](i+K≤N). In other words, P is a substring of S, of which starting address is within [0...i−1], and P is also a prefix of S[i...N−1]. If P exists, Alice will append integer K and T to ciphertext. If T is not unique, Alice would select the minimal one. And then i is incremented by K. If P does not exist, Alice will append -1 and the ASCII code of letter S[i] to ciphertext, and then increment i by 1.
Obviously the first letter cannot be encrypted. That is to say, P does not exist when i=0. So the first integer of ciphertext must be -1, and the second integer is the ASCII code of S[0].
When i=N, all letters are encrypted, and Alice gets the final ciphertext, which consists of many pairs of integers. Please help Alice to implement this method.
aaaaaa
aaaaabbbbbaaabbc
-1 97
5 0
Case #2:
-1 97
4 0
-1 98
4 5
5 2
-1 99
#include <bits/stdc++.h>
using namespace std;
struct SAM
{
static const int MAXN = <<;//大小为字符串长度两倍
static const int LetterSize = ; int tot, last, ch[MAXN][LetterSize], fa[MAXN], len[MAXN];
int sum[MAXN], tp[MAXN], cnt[MAXN]; //sum,tp用于拓扑排序,tp为排序后的数组
int ft[MAXN];
void init( void)
{
last = tot = ;
len[] = ;
memset( ch[], , sizeof ch[]);
} void add( int x, int pos)
{
int p = last, np = last = ++tot;
len[np] = len[p] + , cnt[np] = , ft[np] = pos;
memset( ch[np], , sizeof ch[np]);
while( p && !ch[p][x]) ch[p][x] = np, p = fa[p];
if( p == )
fa[np] = ;
else
{
int q = ch[p][x];
if( len[q] == len[p] + )
fa[np] = q;
else
{
int nq = ++tot;
memcpy( ch[nq], ch[q], sizeof ch[q]);
len[nq] = len[p] + , fa[nq] = fa[q], fa[q] = fa[np] = nq, ft[nq] = ft[q];
while( p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];
}
}
} void toposort( void)
{
for(int i = ; i <= len[last]; i++) sum[i] = ;
for(int i = ; i <= tot; i++) sum[len[i]]++;
for(int i = ; i <= len[last]; i++) sum[i] += sum[i-];
for(int i = ; i <= tot; i++) tp[sum[len[i]]--] = i;
for(int i = tot; i; i--) ft[fa[i]] = min(ft[fa[i]],ft[i]);
} void go(char *ss)
{
toposort();
for(int i=,n=len[last],p,j;i<n;i++)
{
for(p=,j=;;j++)
{
int c=ss[i+j]-'a';
if(!(i+j<n&&ch[p][c]&&ft[ch[p][c]]-j<i))
break;
p=ch[p][c];
}
j--;
if(p==)
printf("-1 %d\n",(int)ss[i]);
else
printf("%d %d\n",j+,ft[p]-j),i+=j;
}
}
} sam; char ss[]; int main(void)
{
//freopen("in.acm","r",stdin);
int t,cs=;cin>>t;
while(t--)
{
sam.init();
scanf("%s",ss);
for(int i=,len=strlen(ss);i<len;i++) sam.add(ss[i]-'a',i);
printf("Case #%d:\n",cs++);
sam.go(ss);
}
return ;
}
hdu5558 Alice's Classified Message的更多相关文章
- HDU5558 Alice's Classified Message(合肥区域赛 后缀数组)
当初合肥区域赛的题(现场赛改了数据范围就暴力过了),可惜当初后缀数组算法的名字都没听过,现在重做下. i从1到n - 1,每次枚举rank[i]附近的排名,并记录当起点小于i时的LCP(rank[i] ...
- (HDU 5558) 2015ACM/ICPC亚洲区合肥站---Alice's Classified Message(后缀数组)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5558 Problem Description Alice wants to send a classi ...
- HUID 5558 Alice's Classified Message 后缀数组+单调栈+二分
http://acm.hdu.edu.cn/showproblem.php?pid=5558 对于每个后缀suffix(i),想要在前面i - 1个suffix中找到一个pos,使得LCP最大.这样做 ...
- 【HDOJ5558】Alice's Classified Message(后缀数组)
题意:给定一个长度为n的下标从0开始的小写字母字符串,每次对于当前的i寻找一个j使得后缀i与后缀j的lcp最大,(j<i)若lcp相同则取较小的 若lcp为0则输出0与当前字符,i自增1,否则输 ...
- Alice's Classified Message HDU - 5558 后缀自动机求某个后缀出现的最早位置
题意: 给定一个长度不超过 10W 的只包含小写字母的字符串,从下标 0 到 n−1.从下标 0 开始操作, 每次对于下标 pos查找下标 pos 开始的子串中最长的在其他地方出现过的长度,其他出现的 ...
- HDU 5558 Alice's Classified Message(后缀数组+二分+rmq(+线段树?))
题意 大概就是给你一个串,对于每个\(i\),在\([1,i-1]\)中找到一个\(j\),使得\(lcp(i,j)\)最长,若有多个最大\(j\)选最小,求\(j\)和这个\(lcp\)长度 思路 ...
- hdu5558 后缀数组
Alice's Classified Message Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- CSP应用开发-CryptAPI函数库介绍
基本加密函数为开发加密应用程序提供了足够灵活的空间.所有CSP的通讯都是通过这些函数.一个CSP是实现所有加密操作的独立模块.在每一个应用程序中至少需要提供一个CSP来完成所需的加密操作.如果使用多于 ...
- How the Bitcoin protocol actually works
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
随机推荐
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- BEGIN_MESSAGE_MAP(Caccess_test_1Dlg, CDialogEx)
BEGIN_MESSAGE_MAP(...消息映射宏的一部分.ON_WM_CREATE()产生一个消息处理函数映射项目,把WM_CREATE和OnCreate函数联系起来. 参数的个数和类型是系统已经 ...
- Spring_day04--SSH框架整合过程
SSH框架整合过程 第一步 导入jar包 第二步 搭建struts2环境 (1)创建action,创建struts.xml配置文件,配置action (2)配置struts2的过滤器 第三步 搭建hi ...
- iOS Web开发
1.让web页面的输入框是数字键盘 html 中 input 的 type = "tel"
- mybatis的一对多,多对一,以及多对对的配置和使用
1.本文章是无意中看见易百教程的Mybatis教程才注意到这个问题,平时都仅仅是在用CRUD,忽略了这方面的问题,真实十分羞愧 2.首先我们开始对mybatis的一对多的探究 根据这个应用场景 ...
- unicode转换中文
<!doctype html><html lang="en"> <head> <meta http-equiv="Refres ...
- netty4.x 实现接收http请求及响应
参考 netty4.x 实现接收http请求及响应 - En taro tassadar - CSDN博客 https://blog.csdn.net/sinat_39783636/article/d ...
- MICRO-SERVICE
这个缩放立方体 (scale cube) 演示了各种可用的缩放选项: 缩放立方体 X 轴缩放 分担了应用程序的多个副本之间的工作负载,通常会使用一个负载平衡器或一个集群管理器. Y 轴缩放 将应用程序 ...
- 网络编程 - 1.简单的套接字通信/2.加上通信循环/3.bug修复/4.加上链接循环/5.模拟ssh远程执行命令
1.简单的套接字通信 服务端 ''' 服务端 接电话 客户端 打电话 1.先启动服务端 2.服务端有两种套接字 1.phone 用来干接收链接的 2.conn 用来干收发消息的 ''' import ...
- 模拟hadoop-rpc通信
一.RPC服务类 package com.css.rpc.server; import java.io.IOException; import org.apache.hadoop.HadoopIlle ...