首页 > 其他分享 >ACdream 1430 SETI

ACdream 1430 SETI

时间:2022-11-09 18:34:38浏览次数:49  
标签:1430 int SETI -- maxn sa message ACdream rk


Description



      Amateur astronomers Tom and Bob try to find radio broadcasts of extraterrestrial civilizations in the air. Recently they received some strange signal and represented it as a word consisting of small letters of the English alphabet. Now they wish to decode the signal. But they do not know what to start with.
      They think that the extraterrestrial message consists of words, but they cannot identify them. Tom and Bob call a subword of the message a potential word if it has at least two non-overlapping occurrences in the message.

      For example, if the message is “abacabacaba”, “abac” is a potential word, but “acaba” is not because two of its occurrences overlap.
      Given a message m help Tom and Bob to find the number of potential words in it.


Input



      Input file contains one string that consists of small letters of the English alphabet. The length of the message doesn’t exceed 10 000.


Output



      Output one integer number — the number of potential words in a message.


Sample Input



abacabacaba


Sample Output



15


后缀数组

#include<iostream>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10005;
const int bit = 256;
class suffix
{
private:
char s[maxn];
int r[maxn], w[maxn], ss[maxn], h[maxn];
int sa[maxn], rk[maxn + maxn], size;
int size1;
public:
bool get(){
if (scanf("%s", s + 1) != 1) return false;
size = strlen(s + 1);
return true;
}
void pre()
{
memset(rk, 0, sizeof(rk));
for (int i = 1; i <= bit; i++) w[i] = 0;
for (int i = 1; i <= size; i++) w[(int)s[i]]++;
for (int i = 1; i <= bit; i++) w[i] += w[i - 1];
for (int i = size; i; i--) sa[w[(int)s[i]]--] = i;
for (int i = 1, j = 1; i <= size; i++)
rk[sa[i]] = (s[sa[i]] == s[sa[i + 1]] ? j : j++);
for (int j = 1; j < size; j += j)
{
for (int i = 1; i <= size; i++) w[i] = 0;
for (int i = 1; i <= size; i++) w[rk[i + j]]++;
for (int i = 1; i <= size; i++) w[i] += w[i - 1];
for (int i = size; i; i--) ss[w[rk[i + j]]--] = i;

for (int i = 1; i <= size; i++) w[i] = 0;
for (int i = 1; i <= size; i++) w[rk[ss[i]]]++;
for (int i = 1; i <= size; i++) w[i] += w[i - 1];
for (int i = size; i; i--) sa[w[rk[ss[i]]]--] = ss[i];

for (int i = 1, k = 1; i <= size; i++)
r[sa[i]] = (rk[sa[i]] == rk[sa[i + 1]] && rk[sa[i] + j] == rk[sa[i + 1] + j]) ? k : k++;
for (int i = 1; i <= size; i++) rk[i] = r[i];
}
for (int i = 1, k = 0, j; i <= size; h[rk[i++]] = k)
for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; k++);
}
void work()
{
int ans = 0, i, l, r;
for (i = 1; i + i <= size; i++)
{
l = 0x7FFFFFFF, r = 0;
for (int j = 2; j <= size; j++)
{
if (h[j] < i)
{
if (r - l >= i) ans++;
l = 0x7FFFFFFF, r = 0;
}
else
{
r = max(r, max(sa[j - 1], sa[j]));
l = min(l, min(sa[j - 1], sa[j]));
}
}
if (r - l >= i) ans++;
}
printf("%d\n", ans);
}
}f;

int main()
{
while (f.get())
{
f.pre();
f.work();
}
return 0;
}



标签:1430,int,SETI,--,maxn,sa,message,ACdream,rk
From: https://blog.51cto.com/u_15870896/5837724

相关文章