Issue: Given a string A, ask if all the characters of string b appear in the character A's character set, (in order to facilitate explanation, characters are uppercase characters)
Write the function bool Isstringcontain (string &a,string &b);
such as String a= ' ABCDEF ', b= ' BCD ', return true;
A= ' ABCDEF ', b= ' BDG ', return false;
A= ' ABCDEF ', b= ' AAA ', return true;
A beginner may use a two-tier loop traversal to resolve:
1 intIsstringcontain (String &a,string &B)2 {3 intJ;4 for(intI=0; I<b.length (); i++)5 {6 for(j=0; J<a.length () && (A[j]!=b[i]); j + +)//the same will jump out of this cycle7 {8 if(J= (a.length-1))//find the last one and a different one, proving a B-character mismatch9 return false;Ten } One } A return true;//b All the proofs are in a . -}
But this time complexity is O (MN), two traversal and no optimization ~
Method Two: Tag Array method
In fact, the question asks whether the lookup is included, and does not specify its character occurrences and order.
We can use an array to store whether a character in a string appears, mark it as ' 1 ', and then directly determine if the character in B matches a.
intIsstringcontain (String &a,string &c) { Charflag[ -];//used to mark the array, easy to set into characters (C language no bool array can only emulate the bool array) inti; for(i=0; I<a.length (); i++) {Flag[a[i]-'A']='1';//a[i]-' A ' represents the order of the character in 26 letters} for(i=0; I<b.length (); i++){ if(flag[b[i]-'A']!='1')//The character in B is not marked in the flag character set, and the character is not a character in the character set of a, which returns false directly; return 0;//0 represents not}return 1;//after finding, the characters in B are all in a, and return 1 is the containing set}
Method Three: Bitwise arithmetic
In the method two, we use the idea of marking, the optimization of the algorithm is how to optimize the tag array and its tagging mode.
In fact, the computer is originally a binary number, the natural 0.1 mark bit. Then it's natural to think of using bit arithmetic to optimize the tag array.
The key is how many bits of binary we need to tag.
All uppercase letters in the set, then there are 26 letters, as long as the number of bits is greater than 26//When the demand expands, remember to expand the number of markers
Lenovo to the C language int data is 4 bytes, 8 bits per byte (I only need to use an int type to complete the tag length)
The character is the first number in the alphabet I'll mark the first few ~
Mark with or operations to ensure that the data previously marked is not lost
Then I use it directly with the operation, as long as not all 0 is the match success.
intIsstringcontain (CharA[],Charb[]) { inti; inttable=0; for(i=0; I<strlen (a); i++) {Table|=(1<< (a[i]-'A'));//Select left shift and each time or operation retention tag } for(i=0; I<strlen (b); i++) { if(Table & (1<< (b[i]-'A')))==0)//The character obtained in B must be 00000100000 after the left shift (with only one 1) data, so the phase is as long as the table if the bit is 1 and the result is 1, the result is not 0. { return 0; } } return 1;}
Final complete code:
#include <stdio.h>#include<string.h>intIsstringcontain (CharA[],Charb[]);intMain () {intflag=0; Chara[ -]; Charb[ -]; scanf ("%s", A); scanf ("%s", B); Flag=isstringcontain (&A,&B); if(flag==1) {printf ("true\n"); } Else{printf ("false\n"); }}intIsstringcontain (CharA[],Charb[]) { inti; inttable=0; for(i=0; I<strlen (a); i++) {Table|=(1<< (a[i]-'A')); } for(i=0; I<strlen (b); i++) { if(Table & (1<< (b[i]-'A')))==0) { return 0; } } return 1;}
This is a better algorithm to learn, write to share, if there are errors please point out, thank you ~
String inclusion issues