Sdut 2772 Data Structure Experiment string one: KMP simple application

Source: Internet
Author: User

The data structure experiment string one: KMP simple applicationTime limit:1000ms Memory limit:65536kb SubmitStatisticProblem description given two strings string1 and string2, determine if string2 is a substring of string1. Input inputs contain multiple sets of data, each set of test data contains two rows, the first row represents string1 (less than 1000000), the second line represents string2 (less than 1000000), and no spaces are guaranteed in string1 and string2. Output for each set of input data, if string2 is a substring of string1, the position of the string2 in string1, if not, output-1. Example Input
Abca12345645abcddd
Example Output
14-1
DQE:KMP The classic application of the algorithm, you can calculate or consult the relevant data.
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 intStrlenChar*s)7 {8     intI=0;9      while(s[i]!=' /')Ten     { Onei++; A     } -     returni; - } the  - voidCnext (Char*s,int*next) - { -     intL=strlen (s); +     intI=0, j=-1; -next[i]=J; +      while(i<l-1) A     { at         if(j==-1|| s[i]==S[j]) -         { -i++; -J + +; -next[i]=J; -         } in         Else -         { toj=Next[j]; +         } -     } the } *  $ intKmpChar*S1,Char*S2,int*next)Panax Notoginseng { -     intL1=strlen (S1), l2=strlen (S2); the     intI=0, j=0; +      while(i<l1&&j<L2) A     { the         if(j==-1|| s1[i]==S2[j]) +         { -i++; $J + +; $         } -         Else -         { thej=Next[j]; -         }Wuyi     } the     if(j>=L2) -         returni-l2+1; Wu     return-1; - } About  $ intMain () - { -     Static Chars1[1000010],s2[1000010]; -     Static intnext[1000010];//This is not exactly equivalent to placing a global variable with static A      while(Gets (S1)) +     { the gets (S2); - Cnext (s2,next); $printf"%d\n", KMP (S1,s2,next)); the     } the     return 0; the } the  - /*************************************************** in User Name: * * * the result:accepted the Take time:68ms About Take memory:1012kb the Submit time:2016-11-02 21:33:59 the ****************************************************/

Sdut 2772 Data Structure Experiment string one: KMP simple application

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.