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
Data range and Tips
Data Size & Hint
The length of the string does not exceed 100
Pascal user note: Two strings may contain multiple spaces
Category labels
Tags Click here to expand
Note: It is still a template, but this time it is self-played, found some problems, WA two times because the output SB 233
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 Chara[ the],b[ the];8 intlena,lenb,sp[ the];9 Ten voidCALCSP () One { A intT; -sp[0]=-1; - for(intI=0; i<lena;i++) the { -t=Sp[i]; - while(t!=-1&& A[t]!=a[i]) t=sp[t];//Note is && -sp[i+1]=++T; + } - } + A voidKMP () at { - intt=0, k=0; - CALCSP (); - while(T<lena && k<LenB) - { - if(k==-1|| A[T]==B[K]) t++,k++;//note Match k==-1 from scratch in Elsek=Sp[k]; - if(K==LENB) cout<<t-lenb+1<<endl,t=0x7f; to } + } - the intMain () * { $Cin>>a>>b;Panax NotoginsengLena=strlen (a); -lenb=strlen (b); the KMP (); + return 0; A}
View Code
"Codevs" 1204 looking for string position