source: CIVL/examples/verifyThisProblems/lrs.c@ 63a79f0

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

add one verify this problem

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

  • Property mode set to 100644
File size: 1.7 KB
Line 
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
9int 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
17int 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
33void 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
44int lcp2(int *a, int n, int index, int* suffixes){
45 return lcp1(a,n,suffixes[index], suffixes[index-1]);
46}
47
48/**
49result[0]: index
50result[1]: length
51*/
52void 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
67int 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}
Note: See TracBrowser for help on using the repository browser.