source: CIVL/examples/verifyThisProblems/lrs.c@ 724ceb2

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

add comment for verifyThis problem

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

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