source:
CIVL/examples/verifyThisProblems/lcp.c@
515b79e
| Last change on this file since 515b79e was 0ae124a, checked in by , 10 years ago | |
|---|---|
|
|
| File size: 808 bytes | |
| Rev | Line | |
|---|---|---|
| [260762f] | 1 | /* |
| [0ae124a] | 2 | author: Yihao |
| [c245979] | 3 | |
| 4 | Link(LCP.zip): http://fm2012.verifythis.org/challenges | |
| 5 | ||
| [260762f] | 6 | Longest Common Prefix (LCP) |
| 7 | Input: an integer array X1[n], and two indices x and y into this array | |
| 8 | Output: length of the longest common prefix of the subarrays of a | |
| 9 | starting at x and y respectively. | |
| 10 | */ | |
| [f3368e99] | 11 | |
| 12 | #include <civlc.cvh> | |
| 13 | ||
| [260762f] | 14 | $input int N_BOUND=4; |
| 15 | $input int n; | |
| 16 | $input int x; | |
| 17 | $input int y; | |
| 18 | $input int X1[n]; | |
| 19 | ||
| 20 | $assume (x < n && y < n && x >=0 && y>=0 && n > 0 && n <= N_BOUND); | |
| 21 | ||
| [0ae124a] | 22 | int lcp(int *arr, int n, int x, int y) { |
| [260762f] | 23 | int l=0; |
| [c245979] | 24 | |
| [260762f] | 25 | while (x+l<n && y+l<n && arr[x+l]==arr[y+l]) { |
| 26 | l++; | |
| 27 | } | |
| 28 | return l; | |
| 29 | } | |
| 30 | ||
| [0ae124a] | 31 | void main() { |
| [260762f] | 32 | int result = lcp(X1, n, x, y); |
| [c245979] | 33 | |
| [260762f] | 34 | $assert($forall {i = 0 .. (result-1)} X1[x+i] == X1[y+i]); |
| [c245979] | 35 | |
| [260762f] | 36 | int maxXY = x > y ? x : y; |
| [c245979] | 37 | |
| [0ae124a] | 38 | if(result + maxXY < n) { |
| [260762f] | 39 | $assert(X1[x+result] != X1[y+result]); |
| 40 | } | |
| 41 | } |
Note:
See TracBrowser
for help on using the repository browser.
