Title Description
Description
Given the string A and string B, to ensure that B is a substring of a, please output B in a where the first occurrence.
Enter a description
Input Description
Only one row contains two strings A and b
Output description
Output Description
One-line integer only
Sample input
Sample Input
ABCD BC
Sample output
Sample Output
2
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>#include<vector>#include<queue>using namespacestd;Const intMAXN =2005;intN,M,NEXT[MAXN];CharT[MAXN],P[MAXN];voidinput () {CIN>>t>>p; N=strlen (t); M=strlen (P);}voidMake () {next[0] =0; for(inti =1, k =0; I < m;i++){ while(k >0&& p[i]! = p[k]) k = next[k-1]; if(P[i] = = P[k]) k++; Next[i]=K; }}voidKMP () { for(inti =0, k =0; I < n;i++){ while(k >0&& T[i]! =P[k]) {k= next[k-1]; } if(T[i] = = P[k]) k++; if(k = =m) {cout<<i-m +2; return; } }}intMain () {input (); Make (); KMP (); return 0;}
codevs1204 Finding substring locations