#Does the string on the fourth day of C language practice contain supplementary questions

Posted by filch on Wed, 09 Feb 2022 17:41:33 +0100

Does the string on the fourth day of C language practice contain supplementary questions

Title Description:
Suppose there is A string A composed of various letters and another string B. the number of letters of B in the string is relatively small. What is the fastest way to find all the letters in small string B in large string A?

For example, if it is the following two strings:
String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPO
The answer is true, and all the letters string1 in string2 also have it.

If it is the following two strings:
String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPZ
The answer is false because the Z letter in the second string is not in the first string.

The questions and main ideas come from July
Link: link.

The third method is hash table method

This method is actually to use a matrix, each element represents a letter
First set all elements to 0;
Set STR2 to scan every element of the matrix
The following example shows eg. str2 [] = "ABCDE"; str1[] = “ABCDEF”;

Let the matrix be a[26] = {0}; < |---- > Use this formula to calculate the address str2[2] - 'A' = 1 of the array corresponding to each element
Therefore, the array element corresponding to the second element is a[1]
Each scanned letter num++
The list is as follows

letterCorresponding elementnumerical value
Aa[0]1
Ba[1]1
Ca[2]1
Da[3]1
Ea[4]1
Fa[5]0

In this way, scan str1 again. If the value of the corresponding position of the letter in a [] is 1, set the value of the position to 0 and num –; Until num = 0, it means that all elements in str2 have been found in str1 and return 1
If the value of num is not 0 until the end of the loop, at least one element in str2 is greater than or equal to that in str1
Description: str1 cannot contain str2. Print false and return 0;

This algorithm is relatively easy to understand and its complexity is only o(m+n), which is recommended, but its memory occupation rate of the computer is high, that is, the space complexity is high

The code is as follows

//Hash table comparison method
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void hashtable(char* s1, char* s2){
	int hash[26] = { 0 };
	int i = 0;
	int num = 0;
	int hashb = 0;
	for (i = 0; i < strlen(s2); i++){
		hashb = s2[i] - 'A';
		if (hash[hashb] == 0){
			hash[hashb] = 1;
			num++;
		}
	}
	for (i = 0; i < strlen(s1); i++){
		hashb = s1[i] - 'A';
		if (hash[hashb] == 1){
			hash[hashb] = 0;
			num--;
		}
		if (num == 0) break;
	}
	if (num == 0) puts("true");
	else puts("false");

	return;
}
//Test function
int main(void){
	char str1[] = "ABCDEFGHLMNOPQRS";
	char str2[] = "DCGSRQPO";
	
	//Hashtable comparison method
	hashtable(str1, str2);
	return 0;
}

The fourth method is the prime number method

This method is to correspond each letter of A~Z to a prime number, because the divisor of prime number is only 1 and itself
Scan str1 stores the product of the prime number corresponding to each element in a long shaping (because the last number will be large, the unsigned long int type is selected in this example), and then scan str2 to remove the prime value corresponding to the element in str2 with the above product value (set as product in this example)
If it can be divided by whole, it means that this element is in str1. If not, this element is not in str1
If you encounter an element that cannot be divided by an integer, print flash
What if all elements can be divisible? Print true

The complexity of this algorithm is dynamic because it does not scan all elements in str2
So the complexity is o(n) ~ o(m+n)
The code is as follows

//Prime number method
//This function is used to judge prime numbers
//Algorithm disadvantage: when the string is too long, you need to define an ultra long type of shaping to store data
//Moreover, it is often difficult to store data, which is not suitable for the case of long string
//For example, test string
	/*char str1[] = "DCGSRQPOABCDEFGHI";
	  char str2[] = "DCGSRQPO";*/
//In this way, false will be displayed, but true will be displayed when the actual program runs correctly
//The reason may be that the data type of unsigned long int cannot carry the size of product
//If the test string is
	/*char str1[] = "DCGSRQPOABCDEFGH";
	  char str2[] = "DCGSRQPO";*/
//In this way, the program will run correctly
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int my_prine_num(int x){
	int i = 0;
	if(x == 2) return x;
	for(i = 2;i <= x/2;i++){
		if(x%i == 0){
			return 0;
		}
	}
	return x;
}
int prime_num_cp(char* s1,char* s2){
	//Find 24 prime numbers and assign them to A ~ Z, because this step can assign them in advance
	//So it's not included in the complexity
	int i = 0;
	int n = 0;
	int num_letter[26] = {0};
	//Assign values to 26 letters and store the values in num_ In letter
	for(i = 2;n < 26;i++){
		if(my_prine_num(i) != 0){
			num_letter[n] = my_prine_num(i);
			n++;
		}
	}

	//Calculate the complexity from here
	//Go through the long string and calculate the product of prime numbers
	int index = 0;
	unsigned long int product = 1;
	for(i = 0;i < strlen(s1);i++){
		index = s1[i] - 'A';
		product = num_letter[index] * product;
	}

	//Go through the short string to see if it can be divided
	for(i = 0;i < strlen(s2);i++){
		index = s2[i] - 'A';
		if(product%num_letter[index] != 0){
			puts("false");
			return 0;
		}
	}
	
	//Indicates that all elements of the short string have been experienced and the corresponding prime can be changed
	//product division
	puts("ture");
	return 1;

	//So the total complexity is o(n) ~ o(m+n)
}


//Test function
int main(void){
	char str1[] = "ABCDEFGHLMNOPQRS";
	char str2[] = "DCGSRQPO";

	//Prime number method
	prime_num_cp(str1,str2);
	return 0;
}

The disadvantage of this algorithm is that if str1 is too long, the prime product that needs to be stored is very large, and a data type that can store a large number is required
For example, in this case

Test string
char str1[] = "DCGSRQPOABCDEFGHI";
char str2[] = "DCGSRQPO";
In this way, false will be displayed, but true will be displayed when the actual program runs correctly
The reason may be that the data type of unsigned long int cannot carry the size of product
If the test string is
char str1[] = "DCGSRQPOABCDEFGH";
char str2[] = "DCGSRQPO";
In this way, the program will run correctly

Therefore, this algorithm is not suitable for comparing character replacement when character replacement is too long

Whether the string contains problems, bloggers can understand and deal with these methods. If there are better algorithms or code errors, you are welcome to comment and prove that I am a sleeping slot. You have 120 kg. See you next question

This article is only used for C language continuous and sharing excellent algorithms

Topics: C Algorithm string