标签:namespace name bsp end max cin pre ret next
#include <iostream> #include <cstring> using namespace std; const int maxn = 1000000+7; char s[maxn], p[maxn]; int net[maxn]; int lens, lenp; void getnext() { net[0] = -1; int i = 0,j = -1; while (i < lenp - 1) { if (j == -1 || p[i] == p[j]) net[++i] = ++j; else j = net[j]; } } int kmp() { lens = strlen(s); lenp = strlen(p); int i = 0, j = 0; getnext(); while (i < lens && j < lenp) { if (s[i] == p[j] || j == -1) i++, j++; else j = net[j]; } if (j == lenp) return i - j; else return -1; } int main() { cin >> s; cin >> p; int ans = kmp(); cout << ans << endl; return 0; }
标签:namespace name bsp end max cin pre ret next
原文地址:https://www.cnblogs.com/wronin/p/11422394.html