source: CIVL/examples/verifyThisProblems/lrs.c@ 49baa33

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 49baa33 was 11d2b47, checked in by Yihao Yan <yihaoyan1@…>, 10 years ago

minor changes

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@3064 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2Note: This problem has not been solved yet
3
4This is a problem in 2012 as an advance problem for LCP
5
6Here is the description:
7Together with a suffix array, LCP can be used to solve interesting text
8problems, such as finding the longest repeated substring (LRS) in a text.
9
10A suffix array (for a given text) is an array of all suffixes of the
11text. For the text [7,8,8,6], the suffix array is
12[[7,8,8,6],
13 [8,8,6],
14 [8,6],
15 [6]]
16
17Typically, the suffixes are not stored explicitly as above but
18represented as pointers into the original text. The suffixes in a suffix
19array are sorted in lexicographical order. This way, occurrences of
20repeated substrings in the original text are neighbors in the suffix
21array.
22
23For the above, example (assuming pointers are 0-based integers), the
24sorted suffix array is:
25
26[3,0,2,1]
27*/
28
29#include <stdlib.h>
30#include <stdio.h>
31#include <civlc.cvh>
32#include <assert.h>
33
34$input int N=4;
35$input int X1[N];
36
37int 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
45int 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
61void 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
72int lcp2(int *a, int n, int index, int* suffixes){
73 return lcp1(a,n,suffixes[index], suffixes[index-1]);
74}
75
76/**
77result[0]: index
78result[1]: length
79*/
80void 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
95int 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];
103 $assert($exists {int k | k >= 0 && k < N - maxLen && k != maxLen}(
104 $forall {i = 0 .. maxLen} X1[k+i] == X1[index+i]
105 ));
106
107 // printf("index:%d\n", result[0]);
108 // printf("length:%d\n", result[1]);
109 free(result);
110 return 0;
111}
Note: See TracBrowser for help on using the repository browser.