source: CIVL/examples/verifyThisProblems/lcp.c@ 6247c10

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

format changes

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

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/*
2Author: Yihao Yan
3
4Download LCP.zip from: http://fm2012.verifythis.org/challenges
5
6-----------------
7Problem decription:
8
9Longest Common Prefix (LCP)
10Input: an integer array X1[n], and two indices x and y into this array
11Output: length of the longest common prefix of the subarrays of a
12 starting at x and y respectively.
13
14-----------------
15Verification task:
16
17Implement the lcp function, and verify that it behaves in the way described above.
18
19command: civl verify lcp.c
20
21result:
22For arrays with length less than 5, lcp function returns an integer n which is
23the length of the longest common prefix between its two suffixes: The two suffixes
24are the same with indexes from 0 to n-1 while they are different at index n.
25*/
26
27#include <civlc.cvh>
28
29$input int N_BOUND=4;
30$input int n;
31$input int x;
32$input int y;
33$input int X1[n];
34
35$assume (x < n && y < n && x >=0 && y>=0 && n > 0 && n <= N_BOUND);
36
37int lcp(int *arr, int n, int x, int y){
38 int l=0;
39
40 while (x+l<n && y+l<n && arr[x+l]==arr[y+l]) {
41 l++;
42 }
43 return l;
44}
45
46void main(){
47 int result = lcp(X1, n, x, y);
48
49 $assert($forall {i = 0 .. (result-1)} X1[x+i] == X1[y+i]);
50
51 int maxXY = x > y ? x : y;
52
53 if(result + maxXY < n){
54 $assert(X1[x+result] != X1[y+result]);
55 }
56}
Note: See TracBrowser for help on using the repository browser.