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 ... 
随机推荐
- 《Java并发编程实战》第八章 线程池的使用 读书笔记
			一.在任务与运行策略之间的隐性解耦 有些类型的任务须要明白地指定运行策略,包含: . 依赖性任务.依赖关系对运行策略造成约束.须要注意活跃性问题. 要求线程池足够大,确保任务都能放入. . 使用线程封 ... 
- 蓝桥杯 历届试题 剪格子(dfs搜索)
			历届试题 剪格子 时间限制:1.0s 内存限制:256.0MB 问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |* || +--****--+ ||* | ** ... 
- Asp.Net MVC 把PartialView、View转换成字符串
			在开发中有时要在后台获得某个View 或者 PartialView 生成的字符串,只要你熟悉Asp.Net MVC 生命周期就能理解和敲出下面的代码.没什么高深的,直接上代码: 1,输出View H ... 
- 集成学习AdaBoost算法——学习笔记
			集成学习 个体学习器1 个体学习器2 个体学习器3 ——> 结合模块 ——>输出(更好的) ... 个体学习器n 通常,类似求平均值,比最差的能好一些,但是会比最好的差. 集成可能提 ... 
- TArray<uint8>转FString
			void ARamaUDPReceiver::Recv(const FArrayReaderPtr& ArrayReaderPtr, const FIPv4Endpoint& EndP ... 
- android APP上线前,应该准备的东西
			这里给出一些主流的应用市场名单,有些可能已经不行了,自己找一找,很容易的: 应用市场图-1 
- ios UITableView多选删除
			第一步, - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath ... 
- Android实现“退出确认”对话框
			@Override public void onBackPressed() { new AlertDialog.Builder(this).setTitle("确认退出吗?") . ... 
- 详说 CSS 清除浮动
			转自:http://kayosite.com/remove-floating-style-in-detail.html 浮动,从诞生那天起,它就是个特别的属性——既为网页布局带来新的方法,却又随之产生 ... 
- R语言中获取当前目录
			# 获取当前工作目录 getwd() # 设置工作目录 setwd() 
