(string) Word string conjugation

Source: Internet
Author: User

Topic:

Given two strings A and B, Q B is an inflection word for a substring, such as input A=hello,b=lel,lle,ello is true, but B=elo is false. (string is continuous)

Ideas:
    • Sliding window thought: Dynamic maintenance of a "window", such as the length of B is 3, to investigate a[0..2],a[1..3],a[2..4] is B's conjugation, the key is how to compare with B?
    • Hash array Statistics: Based on the particularity of the character, you can use [0,255] array to count the number of occurrences of the characters, assuming all lowercase English letters, then use [0,25] to denote the number of occurrences of each word in B, by recording the number of 0 occurrences of nonzero.
    • Window slide, move right One:

New Window a[i-lenb+1...i], old window a[i-lenb...i-1]

Throw away A[i-lenb], add a[i], specific operation reference Code, text expression unclear.

Code:
#include <iostream>#include<vector>using namespacestd;#defineNUM 26BOOLVariable_bit_word (Char*a,intLenaChar*b,intLenB) {Vector<int> cnt ( -,0); intnonzero=0; //Statistic of B     for(intI=0; i<lenb;i++){        if(++cnt[b[i]-'a']==1) nonzero++; }    //First Slide window     for(intI=0; i<lenb;i++){        intc=a[i]-'a'; --Cnt[c]; if(cnt[c]==0) nonzero--; if(cnt[c]==-1) nonzero++; }    if(nonzero==0)        return true;  for(inti=lenb;i<lena;i++){        intc=a[i-lenb]-'a'; ++Cnt[c]; if(cnt[c]==1) nonzero++; if(cnt[c]==0) nonzero--; C=a[i]-'a'; --Cnt[c]; if(cnt[c]==0) nonzero--; if(cnt[c]==-1) nonzero++; if(nonzero==0)return true; }    return false;}intMain () {Chara[]="Hello"; Charb[]="Lel"; intLena=sizeof(a)/sizeof(a[0])-1; intlenb=sizeof(b)/sizeof(b[0])-1; cout<<variable_bit_word (A,LENA,B,LENB) <<Endl; return 0;}

(string) Word string conjugation

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.