Vijos1425 Sub-string removal

Source: Internet
Author: User

Vijos1425 Sub-string removal

Description:We define string A as a substring of string B when and only if we can find a string in B string. Now give you a string a, and another string B, each time you from the B string from left to right to find the first a string, and delete it from the B string, until a string is not a substring of B string, ask you need to make a few delete operations. Input Format:Input file total 2 lines, the first line a string a (length less than 256), the second row of a string B. 30% of the data is randomly generated, 50% of the data satisfies the input file size less than 300kb;100% data satisfies the input file is less than 500KB, the string A, B will only appear in English letters. output Format:The output file has only one integer n. Examples:Abcabcabcabaabcbccc Sample Description:abcabcabaabcbccc-> abcabaabcbccc-> abaabcbccc-> ababccc-> ABCC Analysis:When I first saw this problem, I immediately thought of the kmp+ two-way list, but because the code implementation is more complex, it gave up the idea. A great God tells us that we can simulate matching operations with stacks, pushing elements into the stack each time to see if the top element of the stack matches the substring, as shown in: For example: substring: ABC string: ABCABABCC. First step: Put a into the stack first, do not match, b into the stack, do not match, c into the stack, match, and pop up the corresponding elements of the stack, ans++.


The second step: put a into the stack, do not match the B into the stack, do not match the A into the stack, do not match the B into the stack, do not match, c into the stack, match, ans++, pop the corresponding element;

The third step: C into the stack, matching, ans++, popup corresponding elements;




The above is the process of using the stack simulation, the following code:
1#include"iostream"2#include"Cmath"3#include"Cstdio"4#include"CString"5 #defineMAXN 100000106 7 using namespacestd;8 CharA[MAXN], B[MAXN], STACK[MAXN];9 intLena, Lenb;Ten  One BOOLMatch (intTop)//matching function A { -  -      for(inti = top, j = Lena; I >=1&& J >=1; I.--j) the     { -         if(A[j]! = Stack[i])return false ; -     } -  +     return true ; -  + } A  at intMain () - { -     Long LongAns =0 ; -scanf"%s%s", A +1, B +1); -Lena = strlen (A +1); -Lenb = strlen (b +1); in     inttop =1 ; -      for(inti =1; I <= Lenb; ++i) to     { +Stack[top] =B[i]; -         if(Stack[top] = = A[lena] && top >= Lena &&match (top)) the         { *ans++; $Top-=Lena;Panax Notoginseng         } -top++; the     } +printf"%i64d", ans); A     return 0 ; the}

Finish

Vijos1425 Sub-string removal

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.