Ural 1590. Bacon’s Cipher 后缀数组
来源:互联网 发布:数据恢复破解版 编辑:程序博客网 时间:2024/06/06 00:28
1590. Bacon’s Cipher
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Programmer Vasya was down on his luck. Instead of a vacation, he was sent to a scientific conference.
“It is necessary to increase your competence,” his boss said, “it’s an important conference on cryptography, and it’s held in France, where they used encryption in the days of de Richelieu and cracked codes in the days of Viete.”
One of the talks at the conference was about the attempts to solve Bacon’s ciphers. The speaker proposed a hypothesis that the key to Bacon’s secrets could be found if all possible substrings of Bacon’s works were analyzed.
“But there are too many of them!” Vasya expressed his astonishment.
“Not as many as you think,” the speaker answered, “count them all and you’ll see it yourself.”
That evening Vasya found on the Web the complete set of Bacon’s works. He wrote a program that converted the texts into one long string by removing all linebreaks, spaces, and punctuation marks. And now Vasya is confused because he doesn’t know how to calculate the number of different substrings of this string.
Input
You are given a nonempty string consisting of lowercase English letters. The string is no longer than 5000 symbols.
Output
Output the number of different substrings of this string.
Sample
aaba
8
Problem Author: Alex Gevak
Problem Source: ACM ICPC 2007–2008. NEERC. Eastern Subregion. Yekaterinburg, October 27, 2007
Problem Source: ACM ICPC 2007–2008. NEERC. Eastern Subregion. Yekaterinburg, October 27, 2007
/** 2013_7_27 * * Ural 1590. Bacon’s Cipher * 所有子串中不相同的个数 * 后缀数组对后缀排序然后再进行后缀讨论 * 倍增算法 r为待匹配数组 n为总长度 m为字符范围 * */#include <stdio.h>#include <stdlib.h>#include <string.h>int const N = 5005;int wa[N], wb[N], ws[N], wv[N];int rank[N], sa[N], height[N], r[N], n, k;int cmp( int* r, int a, int b, int L ){ return r[a]== r[b] && r[a+ L]== r[b+ L];}void da( int* r, int* sa, int n, int m ){ int i, j, p, *x= wa, *y= wb, *t; for( i= 0; i< m; ++i ) ws[i]= 0; for( i= 0; i< n; ++i ) ws[ x[i]= r[i] ]++; for( i= 1; i< m; ++i ) ws[i]+= ws[i-1]; for( i= n- 1; i>= 0; i-- ) sa[ --ws[ x[i] ] ]= i; for( j= 1, p= 1; p< n; j*= 2, m= p ) { for( p= 0, i= n- j; i< n; ++i ) y[p++]= i; for( i= 0; i< n; ++i ) if( sa[i]>= j ) y[p++]= sa[i]- j; for( i= 0; i< n; ++i ) wv[i]= x[y[i]]; for( i= 0; i< m; ++i ) ws[i]= 0; for( i= 0; i< n; ++i ) ws[ wv[i] ]++; for( i= 1; i< m; ++i ) ws[i]+= ws[i-1]; for( i= n- 1; i>= 0; i-- ) sa[ --ws[ wv[i] ] ]= y[i]; t= x, x= y, y= t, p= 1; x[ sa[0] ]= 0; for( i= 1; i< n; ++i ) x[ sa[i] ]= cmp( y, sa[i-1], sa[i], j )? p- 1: p++; }}void callheight( int* r, int*sa, int n ){ int i, j, k= 0; for( i= 1; i<= n; ++i ) rank[ sa[i] ]= i; for( i= 0; i< n; height[ rank[i++] ]= k ) for( k?k--:0, j= sa[ rank[i]- 1]; r[i+k]== r[j+k]; k++ );}char s[5005];int main(){ int i, ans; scanf("%s", s); n = strlen(s); for(i = 0; i < n; i ++) r[i] = (int)s[i]; da(r, sa, n + 1, 265); callheight( r, sa, n ); ans = n*(n+1)/2; for(i = 1; i <= n; i ++) ans -= height[i]; printf("%d\n", ans); return 0;}
- Ural 1590. Bacon’s Cipher 后缀数组
- HDU4144:Bacon's Cipher
- hdu4144 Bacon's Cipher
- HDU4144 Bacon's Cipher
- Ural 1706. Cipher Message 2 后缀数组
- HDU 4144 Bacon's Cipher
- HDU 4144 Bacon's Cipher
- Bacon's Cipher hdu4144 水题
- hdu 4144 Bacon's Cipher
- 【字符串】hdu4144 Bacon's Cipher
- 17.04.29 Bacon's Cipher
- HDU 4144Bacon's Cipher (解码)
- HDU菜鸟杯比赛第二题Bacon's Cipher
- ural 1297 Palindrome 后缀数组
- ural 1297 Palindrome(后缀数组)
- 【后缀数组】【URAL 1297】Palindrome
- URAL 1297 Palindrome 后缀数组
- [URAL 1297]Palindrome(后缀数组)
- centos6.4安装phpmyadmin
- MAXIM DL
- windows服务结构设计小结,一劳永逸(个人拙见,老鸟请指正)
- B’QConf(北京软件质量大会)记
- 手机分配短讯id的面试题目
- Ural 1590. Bacon’s Cipher 后缀数组
- UVa 10785 - The Mad Numerologist
- 安卓与PC网络对接实现视频实时播放
- 实验比较各离散采样算法
- java笔记第七节
- 双数组trie原理
- PHP输出当前进程所有变量 / 常量 / 模块 / 函数 / 类
- 大话数据结构之单链表
- 统计知识选讲(一)——主成分分析(PCA)的思想