"Codevs" 1204 looking for string position

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.