Poj 1743 Musical Theme

Source: Internet
Author: User

Question: Search for the maximum number of duplicate substrings without overlap in the suffix Array

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <vector>
# Include <stack>
# Include <list>
// # Include <iostream>
# Include <map>
# Define M 1000010
Using namespace std;
# Define inf 0x3f3f3f
# Deprecision Max 110
Int max (int a, int B)
{
Return a> B? A: B;
}
Int min (int a, int B)
{
Return a <B? A: B;
}
Int wa [M], wb [M], wv [M], ws [M];
Int rank [M], sa [M], r [M], dp [32] [M], height [M], mm [M];
Int cmp (int * r, int a, int B, int l)
{
Return r [a] = r [B] & r [a + l] = r [B + l];
}
Void da (int n, int m)
{
// Ws [0] = 0;
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];
For (t = x, x = y, y = t, p = 1, x [sa [0] = 0, I = 1; I <n; I ++)
{
X [sa [I] = cmp (y, sa [I], sa [I-1], j )? P-1: p ++;
}
}
}
Void calheight (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 ++ );
}
}
Int check (int k, int n)
{
Int mi = sa [1], ma = sa [1];
For (int I = 2; I <= n; I ++)
{
If (height [I] <k)
{
Mi = ma = sa [I];
}
Else
{
// Printf ("aaa \ n ");
Mi = min (sa [I], mi );
Ma = max (sa [I], ma );
// Printf ("I % d mi % d ma % d \ n", I, mi, ma );
If (ma-mi> = k)
Return 1;
 
}
}
Return 0;
}
Int main ()
{
Int I, left, right, pre, now, n, mid;
While (scanf ("% d", & n), n)
{
Scanf ("% d", & pre );
N --;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & now );
R [I] = now-pre + 100;
Pre = now;
// Printf ("I % d r % d \ n", I, r [I]);
}
R [n] = 0;
Da (n + 1,200 );
 
Calheight (n );
// For (I = 1; I <= n; I ++)
//{
// Printf ("I % d \ n", I, sa [I], height [I]);
//}
Left = 1; right = n/2;
While (left <= right)
{
Mid = (left + right)> 1;
If (check (mid, n) left = mid + 1;
Else
Right = mid-1;
// Printf ("mid % d \ n", mid );
}
If (right> = 4) printf ("% d \ n", right + 1 );
Else
Printf ("0 \ n ");
}
Return 0;
}

Author: Wings_of_Liberty


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.