国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

|算法討論|KMP 學習筆記

2019-11-11 05:44:41
字體:
供稿:網(wǎng)友

輸入兩個字符串s1和s2,輸出s2在s1中出現(xiàn)的位置(位置從0開始) input

abcdefghidefdddeeeffdefeddef

output

3920

實質(zhì)上是MP算法

#include<cstdio> #include<algorithm> #include<cstring> #define ms(i,j) memset(i,j, sizeof i); using namespace std;char s1[1000000 + 5], s2[1000 + 5]; int next[1000 + 5];int n,m; void getNext()//獲得失配函數(shù) { next[0] = 0; next[1] = 0; for (int i=1;i<m;i++) { int j = next[i]; while (j && s2[i]!=s2[j]) j = next[j]; next[i+1] = ((s2[i]==s2[j]) ? (j+1) : (0)); }}void kmp()//在s1里找s2 { getNext(); int j = 0; for (int i=0;i<n;i++) { while (j && s1[i]!=s2[j]) j = next[j]; if (s1[i]==s2[j]) j++; if (j==m) KMP求字符串中的最短周期(循環(huán)節(jié)): poj 2406 Power Strings

#include<cstdio> #include<algorithm> #include<cstring> #define ms(i,j) memset(i,j, sizeof i); using namespace std;const int MAXN = 1000000 + 5;int n;char s[MAXN];int f[MAXN];int main() { while (scanf("%s", s)&&(s[0]!='.')) { n = strlen(s); f[0] = f[1] = 0; for (int i=1;i<n;i++) { int j = f[i]; while (j && s[i]!=s[j]) j = f[j]; f[i+1] = (s[i]==s[j]) ? (j+1) : (0); } int ans = n-f[n]; if (n%ans==0) ans = n/ans; else ans = 1; printf("%d/n", ans); } return 0; }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 南投县| 广东省| 定远县| 炉霍县| 黄陵县| 城口县| 吉林省| 福海县| 万源市| 洛川县| 福泉市| 门头沟区| 敦煌市| 襄城县| 永嘉县| 临猗县| 林西县| 容城县| 东源县| 张家港市| 长春市| 松滋市| 麦盖提县| 罗定市| 吐鲁番市| 吉安市| 乌兰县| 镇原县| 察雅县| 丹巴县| 夏邑县| 宁津县| 茌平县| 开原市| 永昌县| 胶南市| 宜良县| 石楼县| 垦利县| 农安县| 葵青区|