1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | author: Yihao
|
|---|
| 3 |
|
|---|
| 4 | Link(LCP.zip): http://fm2012.verifythis.org/challenges
|
|---|
| 5 |
|
|---|
| 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 | */
|
|---|
| 11 |
|
|---|
| 12 | #include <civlc.cvh>
|
|---|
| 13 |
|
|---|
| 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 |
|
|---|
| 22 | int lcp(int *arr, int n, int x, int y) {
|
|---|
| 23 | int l=0;
|
|---|
| 24 |
|
|---|
| 25 | while (x+l<n && y+l<n && arr[x+l]==arr[y+l]) {
|
|---|
| 26 | l++;
|
|---|
| 27 | }
|
|---|
| 28 | return l;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | void main() {
|
|---|
| 32 | int result = lcp(X1, n, x, y);
|
|---|
| 33 |
|
|---|
| 34 | $assert($forall {i = 0 .. (result-1)} X1[x+i] == X1[y+i]);
|
|---|
| 35 |
|
|---|
| 36 | int maxXY = x > y ? x : y;
|
|---|
| 37 |
|
|---|
| 38 | if(result + maxXY < n) {
|
|---|
| 39 | $assert(X1[x+result] != X1[y+result]);
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.