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
Code:
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace Std;
String A, B;
int n,m,p[105]={0};
int main ()
{
cin>>a;
cin>>b;
N=a.length ();
M=b.length ();
A= "" +A;
B= "" +B;
int j=0;
for (int i=2;i<=m;i++)
{
while (j>0 && b[j+1]!=b[i])
J=P[J];
if (b[i]==b[j+1])
j + +;
P[i]=j;
}
j=0;
for (int i=1;i<=n;i++)
{
while (j>0 && b[j+1]!=a[i])
J=P[J];
if (a[i]==b[j+1])
j + +;
if (j==m)
{
printf ("%d", i-m+1);
Break
}
}
}
Find sub-string position Codevs 1204