描述
You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A (say ai, ai+1 ... aj) is called a "repeated sequence" if it appears more than once in A (there exists some positive k that ai+k = ai, ai+k+1 = ai+1, ... aj+k = aj) and its appearances are not intersected (i + k > j).
Can you find the longest repeated sequence in A?
输入
Line 1: n (1 <= n <= 300), the length of A.
Line 2: the sequence, a1 a2 ... an (0 <= ai <= 100).
输出
The length of the longest repeated sequence.
样例输入
5
2 3 2 3 2
样例输出
2
不知道这题有没有哪位大侠直接暴力过的,感觉n的级别,三次方的话1s有点悬。。。我的想法是,用二分法确定序列最长的长度。然后为了快速判断某一个长度是否成立,建立一个next数组,保存的是从右边开始跟它相等的最近的一个元素的位置,若右方没有与之相等的元素,next数组值为-1,这样在寻找序列起始位置的时候就可以根据next数组直接往下跳了,节省了很多不必要的判断时间。不过这题我当时做的时候考虑不周,在使用next数组往后跳的时候只选择了第一个满足条件的位置,忘了后面还可能有很多个满足条件的位置,只得了90分,结果是WA。当时只剩下十几分钟,就没来得及细想,后来同学问我是怎么做的时候,我才发现这个问题,可惜已经无法提交了。先把比赛时候的代码贴出来吧。
1 #include <cstdio> 2 #include <cstring> 3 4 const int N = 305; 5 6 int n; 7 int seq[N], next[N]; 8 9 // 判断当前长度是否满足条件 10 bool check(int num) 11 { 12 if (0 == num) 13 return true; 14 int i, j, k, res, up = n - (num << 1) + 1; 15 for (i = 1; i <= up; i++) { 16 // next数组为-1,后面没有相等元素,跳过 17 if (-1 == next[i]) 18 continue; 19 j = next[i]; 20 k = j - i; 21 // 序列长度至少为num,才开始比较 22 while (k < num && -1 != next[j]) { 23 k += next[j] - j; 24 j = next[j]; 25 } 26 if (k < num) 27 continue; 28 // 考虑所有满足条件的位置j 29 do { 30 int l = i, r = j; 31 res = 0; 32 while (seq[l] == seq[r] && r <= n) { 33 res++; 34 l++, r++; 35 if (res >= num) 36 return true; 37 } 38 j = next[j]; 39 } while (-1 != j); 40 } 41 return false; 42 } 43 44 int main(void) 45 { 46 int i, j; 47 int left, right, mid; 48 while (EOF != scanf("%d", &n)) { 49 memset(next, -1, sizeof(next)); 50 for (i = 1; i <= n; i++) { 51 scanf("%d", &seq[i]); 52 // 设置元素的next数组,右边与其最接近的相等元素的位置 53 for (j = i - 1; j >= 1; j--) { 54 if (seq[j] == seq[i]) { 55 next[j] = i; 56 break; 57 } 58 } 59 } 60 left = 0, right = (n >> 1) + 1; 61 // 二分法确定满足条件的最长长度 62 while (left <= right) { 63 mid = (left + right) >> 1; 64 if (check(mid)) 65 left = mid + 1; 66 else 67 right = mid - 1; 68 } 69 printf("%d\n", left - 1); 70 } 71 return 0; 72 }