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