Foj 2075 Substring

Source: Internet
Author: User

Calculate the string with the smallest Lexicographic Order that exactly appears n times.

Question: When the suffix array is added with a monotonous stack and n is set to 1, it must be judged. However, the data is a bit watery and can be passed without being judged.

[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>
Using namespace std;
# Define inf 0x3f3f3f
# Define M 100010
Int max (int a, int B)
{
Return a> B? A: B;
}
Int min (int a, int B)
{
Return a <B? A: B;
}
Int height [M], rank [M], r [M], sa [M];
Int ts [M], ta [M], tb [M], TV [M], pos, st, ed;
Struct node
{
Int h, st, w;
} Q [M];
Bool cmp (int * y, int a, int B, int l)
{
Return y [a] = y [B] & y [a + l] = y [B + l];
}
Void da (int n, int m)
{
Int I, j, * x = ta, * y = tb, p;
For (I = 0; I <m; I ++) ts [I] = 0;
For (I = 0; I <n; I ++) ts [x [I] = r [I] ++;
For (I = 1; I <m; I ++) ts [I] + = ts [I-1];
For (I = n-1; I> = 0; I --) sa [-- ts [x [I] = I;
 
For (j = 1, p = 1; p <n; j * = 2, m = p)
{
P = 0;
For (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 <m; I ++) ts [I] = 0;
For (I = 0; I <n; I ++) TV [I] = x [y [I];
For (I = 0; I <n; I ++) ts [TV [I] ++;
For (I = 1; I <m; I ++) ts [I] + = ts [I-1];
For (I = n-1; I> = 0; I --) sa [-- ts [TV [I] = y [I];
Swap (x, y );
X [sa [0] = 0;
P = 1;
For (I = 1; I <n; I ++)
{
If (cmp (y, sa [I-1], sa [I], j) x [sa [I] = p-1;
Else x [sa [I] = p ++;
}
}
}
Void calh (int n)
{
Int I, k, tmp;
For (I = 1; I <= n; I ++) rank [sa [I] = I;
K = 0;
For (I = 0; I <n; I ++)
{
Tmp = sa [rank [I]-1];
For (; r [I + k] = r [tmp + k]; k ++)
;
Height [rank [I] = k;
K? -- K: 0;
}
}
Char s [M];
Int solve (int len, int k)
{
Int I;
Node tmp;
Int top = 0, tail = 0;
Height [len + 1] = 0;
If (k = 1)
{
St = 0, ed = len;
Return 1;
}
For (I = 1; I <= len + 1; I ++)
{
Tmp. h = 0;
Tmp. st = I;
While (top <tail & q [tail-1]. w> = height [I])
{
Tmp. h + = q [tail-1]. h;
Tmp. st = q [tail-1]. st;
Tmp. w = q [tail-1]. w;
If (tmp. h = K-1 & tmp. w> height [I])
{
St = sa [tmp. st];
If (top <tail-1)
Ed = st + max (height [I] + 1, q [tail-2]. w + 1 );
Else
Ed = st + height [I] + 1;
Return 1;
}
Tail --;
}
If (height [I] = 0)
Continue;
Tmp. w = height [I];
Tmp. h + = 1;
Q [tail ++] = tmp;
}
Return 0;
}
Int main ()
{
Int I, k, len;
While (scanf ("% d", & k )! = EOF)
{
Scanf ("% s", s );
Len = strlen (s );
For (I = 0; I <len; I ++)
R [I] = s [I] + 1;
R [len] = 0;
Da (len + 1,200 );
Calh (len );
If (solve (len, k) = 0)
{
Puts ("impossible ");
Continue;
}
For (I = st; I <ed; I ++)
Printf ("% c", s [I]);
Puts ("");
}
}


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.