source: CIVL/examples/verifyThisProblems/lrs.c@ ae55290

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

modify lrs.c verifyThis

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

  • Property mode set to 100644
File size: 2.6 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_BOUND = 4;
33$input int N;
34$input int X1[N];
35$assume (N < N_BOUND && N >= 0);
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 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]
106 ));
107 }else{
108 $assert(index == 0 && maxLen == 0);
109 }
110
111 // printf("index:%d\n", result[0]);
112 // printf("length:%d\n", result[1]);
113 free(result);
114 return 0;
115}
Note: See TracBrowser for help on using the repository browser.