Zoj3395 stammering aliens binary + suffix Array

Source: Internet
Author: User

Stammering aliens

Time Limit: 5 seconds memory limit: 65536 KB

Dr. ellie Arroway has established contact with an extraterrestrial civilization. however, all efforts to decode their messages have failed so far because, as luck wowould have it, they have stumbled upon a race of stuttering aliens! Her team has found out
That, in every long enough message, the most important words appear repeated a certain number of times as a sequence of consecutive characters, even in the middle of other words. furthermore, sometimes they use contractions in an obscure manner. for example,
If they need to sayBabTwice, they might just send the messageBABAB, Which has been abbreviated because the secondBOf the first word can be reused as the firstBOf the second one.

Thus, the message contains possibly overlapping repetitions of the same words over and over again. as a result, Ellie turns to you, S. r. hadden, for help in identifying the gist of the message.

Given an integerM, And a stringS, Representing the message, your task is to find the longest substringSThat appears at leastMTimes. For example, in the messageBaaaababababbababbab, The length-5 wordBABABIs
Contained 3 times, namely at positions 5, 7 and 12 (where indices start at zero ). no substring appearing 3 or more times is longer (see the first example from the sample input ). on the other hand, no substring appears 11 times or more (see example 2 ).

In case there are several solutions, the substring with the rightmost occurrence is preferred (see example 3 ).

Input

The input contains several test cases. Each test case consists of a line with an integerM(M≥1), the minimum number of repetitions, followed by a line containing a stringSOf lengthMAnd 40 000, inclusive.
All characters inSAre lowercase characters from""To"Z". The last test case is denotedM= 0 and must not be processed.

Output

Print one line of output for each test case. If there is no solution, outputNone; Otherwise, print two integers in a line, separated by a space. The first integer denotes the maximum length of a substring appearing at leastMTimes;
The second integer gives the rightmost starting position of this substring.

Sample Input
3baaaababababbababbab11baaaababababbababbab3cccccc0
Sample output
5 12none4 2
There were a lot of people who made this question during the competition that day, so they had to dig their heads and find that they could easily kill the game with a suffix array, I read a lot of papers from the national training team a few days ago. I am a little bit familiar with it. At least I can use the template!
Let's explain the meaning of the question. To give a m and a string, We need to overlap the string with at least m sub-strings and ask which is the longest and rightmost. If no one exists, none is output. First, an array with a suffix is used. I use a large Jilin template. The data is not big, and a simple doubling template (O (nlog (N) is used )); first, we should deal with the special case. It is very simple to check whether the condition with the length of 1 meets the conditions. If the condition is not met, it is none, otherwise, the length must be at least 1. Then we can easily think of the length of the binary enumeration and record the rightmost value! Note that special processing is required when m is set to 1. At this time, it is obvious that strlen (s) and 0 are directly output; # include <iostream> # include <string. h> # include <memory. h >#include <algorithm> using namespace STD; # define n 40010 char s [N]; // n> 256int N, ranks [N], sa [N], height [N], TMP [N], top [N]; void Makesa () {// O (N * log n) int I, j, Len, NA; NA = (n <256? 256: N); memset (top, 0, na * sizeof (INT); for (I = 0; I <n; I ++) top [ranks [I] = s [I] & 0xff] ++; for (I = 1; I <Na; I ++) top [I] + = top [I-1]; for (I = 0; I <n; I ++) sa [-- top [ranks [I] = I; for (LEN = 1; Len <n; Len <= 1) {for (I = 0; I <n; I ++) {J = sa [I]-len; If (j <0) J + = N; TMP [top [ranks [J] ++] = J;} SA [TMP [top [0] = 0] = J = 0; for (I = 1; I <n; I ++) {If (rank S [TMP [I]! = Ranks [TMP [I-1] | ranks [TMP [I] + Len]! = Ranks [TMP [I-1] + Len]) Top [+ + J] = I; SA [TMP [I] = J;} memcpy (ranks, SA, N * sizeof (INT); memcpy (SA, TMP, N * sizeof (INT); If (j> = n-1) break;} void LCP () {// o (4 * n) int I, j, k; For (j = ranks [height [I = k = 0] = 0]; I <n-1; I ++, K ++) while (k> = 0 & S [I]! = S [SA [J-1] + k]) height [J] = (k --), j = ranks [SA [J] + 1];} int times, Len; int check (int l) {int I, CNT, Rp =-1, temp; for (I = 1; I <= Len; I ++) if (height [I]> = L) {CNT = 1; temp = max (SA [I], sa [I-1]); while (I + 1 <= Len & height [I + 1]> = L) {I ++; CNT ++; temp = max (temp, sa [I]);} If (CNT> = times-1) Rp = max (RP, temp);} return RP;} int main () {int I, j, len2; while (scanf ("% d", ×), times) {scanf ("% s", S); n = strlen (s ); if (1 = times) {PR INTF ("% d 0/N", n); continue;} Len = N; s [n ++] = 0; Makesa (); LCP (); int ans; int L = 1, R = Len, mid, maxlen, temp; If (check (1) =-1) {printf ("NONE/N"); continue ;} while (L <= r) {mid = (L + r)> 1; if (temp = check (MID ))! =-1) {ans = temp; maxlen = mid; L = Mid + 1;} else r = mid-1;} printf ("% d/N ", maxlen, ANS);} return 0 ;}

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.