| [63a79f0] | 1 | #include <stdlib.h>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 | #include <civlc.cvh>
|
|---|
| 4 | #include <assert.h>
|
|---|
| 5 |
|
|---|
| 6 | $input int N=4;
|
|---|
| 7 | $input int X1[N];
|
|---|
| 8 |
|
|---|
| 9 | int lcp1(int *arr, int n, int x, int y){
|
|---|
| 10 | int l=0;
|
|---|
| 11 | while (x+l<n && y+l<n && arr[x+l]==arr[y+l]) {
|
|---|
| 12 | l++;
|
|---|
| 13 | }
|
|---|
| 14 | return l;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | int compare(int *a, int n, int x, int y) {
|
|---|
| 18 | if (x == y) return 0;
|
|---|
| 19 | int l = 0;
|
|---|
| 20 |
|
|---|
| 21 | while (x+l<n && y+l<n && a[x+l] == a[y+l]) {
|
|---|
| 22 | l++;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | if (x+l == n) return -1;
|
|---|
| 26 | if (y+l == n) return 1;
|
|---|
| 27 | if (a[x+l] < a[y+l]) return -1;
|
|---|
| 28 | if (a[x+l] > a[y+l]) return 1;
|
|---|
| 29 |
|
|---|
| 30 | return -2;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | void sort(int *a, int n, int *data) {
|
|---|
| 34 | for(int i = 0; i < n + 0; i++) {
|
|---|
| 35 | for(int j = i; j > 0 && compare(a, n, data[j - 1], data[j]) > 0; j--) {
|
|---|
| 36 | int b = j - 1;
|
|---|
| 37 | int t = data[j];
|
|---|
| 38 | data[j] = data[b];
|
|---|
| 39 | data[b] = t;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | int lcp2(int *a, int n, int index, int* suffixes){
|
|---|
| 45 | return lcp1(a,n,suffixes[index], suffixes[index-1]);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | result[0]: index
|
|---|
| 50 | result[1]: length
|
|---|
| 51 | */
|
|---|
| 52 | void lrs(int* a, int n, int *result){
|
|---|
| 53 | int suffixes[n];
|
|---|
| 54 | for(int i=0; i<n; i++){
|
|---|
| 55 | suffixes[i] = i;
|
|---|
| 56 | }
|
|---|
| 57 | sort(a, n, suffixes);
|
|---|
| 58 | for(int i=1; i<n; i++){
|
|---|
| 59 | int len = lcp2(a, n, i,suffixes);
|
|---|
| 60 | if(len > result[1]){
|
|---|
| 61 | result[0] = suffixes[i];
|
|---|
| 62 | result[1] = len;
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | int main(){
|
|---|
| 68 | int* result = (int*)malloc(2* sizeof(int));
|
|---|
| 69 | result[0] = 0;
|
|---|
| 70 | result[1] = 0;
|
|---|
| 71 | // int arr[] = {1,2,3,1,2,3};
|
|---|
| 72 | lrs(X1, N, result);
|
|---|
| 73 | int index = result[0];
|
|---|
| 74 | int maxLen = result[1];
|
|---|
| 75 | $assert($exists {int k | k >= 0 && k < N - maxLen && k != maxLen}(
|
|---|
| 76 | $forall {i = 0 .. maxLen} X1[k+i] == X1[index+i]
|
|---|
| 77 | ));
|
|---|
| 78 |
|
|---|
| 79 | // printf("index:%d\n", result[0]);
|
|---|
| 80 | // printf("length:%d\n", result[1]);
|
|---|
| 81 | free(result);
|
|---|
| 82 | return 0;
|
|---|
| 83 | }
|
|---|