aboutsummaryrefslogtreecommitdiff
path: root/rcc-legacy/tests/sysy_lvX/091_kmp.sy
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-06-08 17:10:31 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-06-08 17:10:31 +0800
commit72af0dedf9e18911da65b9a5c4906689e868d9fc (patch)
tree2ecbad93f96ab8ad7e26dc02cd02d802f853ab97 /rcc-legacy/tests/sysy_lvX/091_kmp.sy
parent8faf6d290d6703b1775e0ad7a8928a4ebea4b11b (diff)
downloadrcc-project-72af0dedf9e18911da65b9a5c4906689e868d9fc.tar.gz
rcc-project-72af0dedf9e18911da65b9a5c4906689e868d9fc.tar.xz
[legacy] Add functional and performance test suites
Diffstat (limited to 'rcc-legacy/tests/sysy_lvX/091_kmp.sy')
-rw-r--r--rcc-legacy/tests/sysy_lvX/091_kmp.sy62
1 files changed, 62 insertions, 0 deletions
diff --git a/rcc-legacy/tests/sysy_lvX/091_kmp.sy b/rcc-legacy/tests/sysy_lvX/091_kmp.sy
new file mode 100644
index 0000000..b3dd7a9
--- /dev/null
+++ b/rcc-legacy/tests/sysy_lvX/091_kmp.sy
@@ -0,0 +1,62 @@
+
+void get_next(int str[], int next[])
+{
+ next[0] = -1;
+ int i = 0, j = -1;
+ while (str[i]) {
+ if (j == -1 || str[i] == str[j]) {
+ j = j + 1;
+ i = i + 1;
+ next[i] = j;
+ }
+ else
+ j = next[j];
+ }
+}
+
+int KMP(int dst[], int src[])
+{
+ int next[4096];
+ get_next(dst, next);
+
+ int i = 0, j = 0;
+ while (src[j]) {
+ if (dst[i] == src[j]) {
+ i = i + 1;
+ j = j + 1;
+ if (!dst[i]) {
+ return j;
+ }
+ } else {
+ i = next[i];
+ if (i == -1) {
+ i = i + 1;
+ j = j + 1;
+ }
+ }
+ }
+ return -1;
+}
+
+int read_str(int buf[])
+{
+ int i = 0;
+ while (1) {
+ buf[i] = getch();
+ if (buf[i] == 10)
+ break;
+ i = i + 1;
+ }
+ buf[i] = 0;
+ return i;
+}
+
+int main()
+{
+ int dst[4096], src[4096];
+ read_str(dst);
+ read_str(src);
+ putint(KMP(dst, src));
+ putch(10);
+ return 0;
+} \ No newline at end of file