KMP算法(尝试)
来源:互联网 发布:js 多选插件 编辑:程序博客网 时间:2024/06/06 02:05
//// main.c// test//// Created by 胡博豪 on 16/2/29.// Copyright © 2016年 胡博豪. All rights reserved.//#include <stdio.h>#include <string.h>#include <stdlib.h>int *next(const char *string) { int *next = (int *)malloc(sizeof(int) * 100); next[0] = next[1] = 0; int i = 0, j = 1; while (++j < strlen(string)) { if (string[i] != string[j]) { next[j] = 0; i = 0; } else { i++; next[j] = next[j - 1] + 1; } } return next;}int KMP(const char *ms, const char *os, const int *next) { int i, j = 0; for (i = 0; i < strlen(ms);) { if (ms[i] == os[j]) { j++; i++; } else { if (j == 0) { i++; } j = next[j]; } if (j == strlen(os)) { return i - j + 1; } } return -1;}int count_str_in_str(char *str_m, const char *str_o) { int count = 0, temp = 0; int *n = next(str_o); char *str_temp = str_m; while (temp != -1) { temp = KMP(str_temp, str_o, n); str_temp += temp + 1; count++; } free(n); return count - 1;}int main(int argc, const char * argv[]) { int n, index; while(~scanf("%d", &n)) { char **sa = (char **)malloc(sizeof(char *) * n); for (index = 0; index < n; index++) { char *temp = (char *)malloc(sizeof(char) * 100); scanf("%s", temp); sa[index] = temp; } char data[100001]; scanf("%s", data); char *re = NULL; int amount = 0; for (index = 0; index < n; index++) { int te = count_str_in_str(data, sa[index]); if (te > amount) { amount = te; re = sa[index]; } } if (amount == 0) { printf("exciting\n"); } else { printf("%s %d\n", re, amount); } for (index = 0; index < n; index++) { free(sa[index]); } free(sa); } return 0;}
0 0
- KMP算法(尝试)
- 算法尝试(一)
- 算法尝试(二)
- kmp算法(转载)
- KMP算法(转)
- (精)kmp算法
- (四)KMP算法
- KMP算法(1)
- KMP算法(2)
- KMP算法(站位)
- KMP算法(1)
- KMP算法(2)
- Period(KMP算法)
- KMP算法(模板)
- KMP算法(二)
- KMP算法(一)
- KMP算法(转)
- KMP算法(一)
- Emacs按键、命令、小技巧-合集
- 《EasyUI + MVC + EF +WCF》——实现对Datagrid中数据的批量修改或保存
- 怎么解决php调用数据的值或者插入数据库的时候乱码的问题
- Java8 的方法引用
- Opening Files-打开文件
- KMP算法(尝试)
- 程序员修炼之道:从小工到专家 之二
- POJ 3040 Allowance
- 我的成长(七)--- 矛盾,才是智慧
- MFC调用对话框dll弹出非模态对话框
- Java NIO:浅析I/O模型
- 代码重构之笔记
- 分享30个开发人员有用的CSS代码片段
- BZOJ-2037 Sue的小球 DP+费用提前