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