| [724ceb2] | 1 | /*
|
|---|
| 2 | This is a problem in 2012 as an advance problem for LCP
|
|---|
| 3 |
|
|---|
| 4 | Here is the description:
|
|---|
| 5 | Together with a suffix array, LCP can be used to solve interesting text
|
|---|
| 6 | problems, such as finding the longest repeated substring (LRS) in a text.
|
|---|
| 7 |
|
|---|
| 8 | A suffix array (for a given text) is an array of all suffixes of the
|
|---|
| 9 | text. For the text [7,8,8,6], the suffix array is
|
|---|
| 10 | [[7,8,8,6],
|
|---|
| 11 | [8,8,6],
|
|---|
| 12 | [8,6],
|
|---|
| 13 | [6]]
|
|---|
| 14 |
|
|---|
| 15 | Typically, the suffixes are not stored explicitly as above but
|
|---|
| 16 | represented as pointers into the original text. The suffixes in a suffix
|
|---|
| 17 | array are sorted in lexicographical order. This way, occurrences of
|
|---|
| 18 | repeated substrings in the original text are neighbors in the suffix
|
|---|
| 19 | array.
|
|---|
| 20 |
|
|---|
| 21 | For the above, example (assuming pointers are 0-based integers), the
|
|---|
| 22 | sorted suffix array is:
|
|---|
| 23 |
|
|---|
| 24 | [3,0,2,1]
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| [63a79f0] | 27 | #include <stdlib.h>
|
|---|
| 28 | #include <stdio.h>
|
|---|
| 29 | #include <civlc.cvh>
|
|---|
| 30 | #include <assert.h>
|
|---|
| 31 |
|
|---|
| [ae55290] | 32 | $input int N_BOUND = 4;
|
|---|
| 33 | $input int N;
|
|---|
| [63a79f0] | 34 | $input int X1[N];
|
|---|
| [ae55290] | 35 | $assume (N < N_BOUND && N >= 0);
|
|---|
| [63a79f0] | 36 |
|
|---|
| 37 | int lcp1(int *arr, int n, int x, int y){
|
|---|
| 38 | int l=0;
|
|---|
| 39 | while (x+l<n && y+l<n && arr[x+l]==arr[y+l]) {
|
|---|
| 40 | l++;
|
|---|
| 41 | }
|
|---|
| 42 | return l;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | int compare(int *a, int n, int x, int y) {
|
|---|
| 46 | if (x == y) return 0;
|
|---|
| 47 | int l = 0;
|
|---|
| 48 |
|
|---|
| 49 | while (x+l<n && y+l<n && a[x+l] == a[y+l]) {
|
|---|
| 50 | l++;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | if (x+l == n) return -1;
|
|---|
| 54 | if (y+l == n) return 1;
|
|---|
| 55 | if (a[x+l] < a[y+l]) return -1;
|
|---|
| 56 | if (a[x+l] > a[y+l]) return 1;
|
|---|
| 57 |
|
|---|
| 58 | return -2;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | void sort(int *a, int n, int *data) {
|
|---|
| 62 | for(int i = 0; i < n + 0; i++) {
|
|---|
| 63 | for(int j = i; j > 0 && compare(a, n, data[j - 1], data[j]) > 0; j--) {
|
|---|
| 64 | int b = j - 1;
|
|---|
| 65 | int t = data[j];
|
|---|
| 66 | data[j] = data[b];
|
|---|
| 67 | data[b] = t;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | int lcp2(int *a, int n, int index, int* suffixes){
|
|---|
| 73 | return lcp1(a,n,suffixes[index], suffixes[index-1]);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | result[0]: index
|
|---|
| 78 | result[1]: length
|
|---|
| 79 | */
|
|---|
| 80 | void lrs(int* a, int n, int *result){
|
|---|
| 81 | int suffixes[n];
|
|---|
| 82 | for(int i=0; i<n; i++){
|
|---|
| 83 | suffixes[i] = i;
|
|---|
| 84 | }
|
|---|
| 85 | sort(a, n, suffixes);
|
|---|
| 86 | for(int i=1; i<n; i++){
|
|---|
| 87 | int len = lcp2(a, n, i,suffixes);
|
|---|
| 88 | if(len > result[1]){
|
|---|
| 89 | result[0] = suffixes[i];
|
|---|
| 90 | result[1] = len;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | int main(){
|
|---|
| 96 | int* result = (int*)malloc(2* sizeof(int));
|
|---|
| 97 | result[0] = 0;
|
|---|
| 98 | result[1] = 0;
|
|---|
| 99 | // int arr[] = {1,2,3,1,2,3};
|
|---|
| 100 | lrs(X1, N, result);
|
|---|
| 101 | int index = result[0];
|
|---|
| 102 | int maxLen = result[1];
|
|---|
| [ae55290] | 103 | if(N > 1){
|
|---|
| 104 | $assert($exists {int k | k >= 0 && k <= N - maxLen && k != index}(
|
|---|
| 105 | $forall {i = 0 .. maxLen-1} X1[k+i] == X1[index+i]
|
|---|
| [63a79f0] | 106 | ));
|
|---|
| [ae55290] | 107 | }else{
|
|---|
| 108 | $assert(index == 0 && maxLen == 0);
|
|---|
| 109 | }
|
|---|
| [63a79f0] | 110 |
|
|---|
| 111 | // printf("index:%d\n", result[0]);
|
|---|
| 112 | // printf("length:%d\n", result[1]);
|
|---|
| 113 | free(result);
|
|---|
| 114 | return 0;
|
|---|
| 115 | }
|
|---|