String constants - determining whether the character string contains `s2` arranged string of` s1`

String constants - Analyzing string s2contains the string s1arrayed

1. String constants (string literal)

Many people do not exist on the C language string type surprised, but the C language provides string constants . The presence of C language string concept: it is the string to NULzero or more bytes of the end of the character. Character strings are typically stored in an array, which is no explicit C language string type of reasons. Since the NULbyte is used to terminate the string, the string can not have internal NULbyte. In general, this restriction does not cause problems. Chose NULterminator as a string, it is because the character is not a printable.

Notation string constant is a string of characters surrounded by double quotes, as follows:

“Hello”
“\aWarning!\a”
“Line 1\nLine2”
“”

String constants (unlike character constant) may be empty. Even empty string is still present as a terminator NULYu section.

literal [ˈlɪtərəl]:adj. 文字的,逐字的,无夸张的
string [strɪŋ]:n. 线,弦,细绳,一串,一行

K & R C:
in the form of string constants in memory, all the characters and NULterminator are stored in a straight position memory. K & R C and does not mention whether a character string constant may be modified in the program, but it clearly shows the different values have the same string constants in memory are stored separately. Therefore, many compilers are allowed to modify string constants.

ANSIC:
ANSIC If you modify the declaration of a string constant, the effect is undefined. It also allows the compiler to a constant string is stored in one place, even if it appears more than once in a program. This makes the modified string constants become extremely dangerous, because a constant modifications could ruin other string constants program. Many ANSI compiler does not allow modify string constants, or option provides compile, let you choose whether to allow modify string constants. In practice, try to avoid doing so. If you need to modify the string, it is stored in the array.

The reason I put the string constants and pointers discussed together, is because it will generate a string constants in the program 指向字符的常量指针. When a string constant appears in an expression, the value of the expression is used to store the addresses of these characters, rather than the characters themselves. So, you can assign a string constant 指向字符的指针, which points to the address stored in these characters. However, you can not assign a string constant array of characters, because the direct value of a string constant is a pointer, not the characters themselves.

If you feel not be assigned or copy the string appears inconvenient, you should know the standard C library contains a set of functions, they are used to manipulate strings, the string including replication, connection, comparison and calculating the string length and lookup function of a specific character in the string.

NULL Pointer, which can be used to represent a zero value.

getsFunction to read a line of text from standard input and stores it in the array is passed to it as an argument. A string of characters entered by a line to a line feed (NEWLINE) end. getsDiscard newline function, and at the end of the line store a NULbyte (a NULbyte refers to an all-zero byte byte mode, similar to '\0'this character constants). Then, getsthe function returns a non- NULLvalue, indicates that the line has been successfully read. When the getsfunction is called input line but in fact does not exist, it returns NULLthe value, which represents the reached the end (end of file) input.

In a C program, working with strings is a common task of the two. Although C language does not exist in stringthe data type, but in the entire language, there is a convention: the string is a string to NULthe end byte character. NULAs a string terminator, which itself is not considered to be part of the string. String constants (string literal) is the source is enclosed in double quotes string of characters. For example, the string constants:

“Hello”

It occupies 6 bytes of space in the memory, respectively in order H, e, l, l, oand NUL.

2. String basis

String data type is an important, but not explicit C language string data type as string constants in the form of a string or a character stored in the array. String constants are applicable to those procedures they will not be modified string. All other strings must be stored in a character array or Yu dynamically allocated memory.

String is a string of zero or more characters, and in a whole bit pattern 0of NULthe end of the byte. Thus, inter-character string contained not appear NULbytes. This restriction rarely cause problems because NULthe byte does not exist associated with it printable characters, and this is the reason it was chosen as the terminator. NULEnd of the string of bytes is, but is not itself part of the string, it does not include the length of the string NULof bytes.

Header file string.hcontains prototypes and declarations required to use string functions. Although not required, but it included in the program this header file is actually a good idea, because it contains the prototype, the compiler can better perform error checking to your program.

NULIt is to focus on ASCII character ’\0’character's name, its byte pattern to all zeros. NULLIt refers to a pointer to a value of 0. They are integer values, whose values are the same, so they may be used interchangeably. However, you should still use the appropriate constant, because it tells people not only use the program to read. This value, and told him the purpose of use of this value.

Symbol NULLin the header file stdio.hdefinition. On the other hand, there is no predefined symbolsNUL, so if you want to use it instead of a character constant’\0’, you have to define your own.

3. Analyzing string s2contains the string s1arrayed

Given two strings s1and s2, write a function to determine s2whether or not comprising s1arrangement. One arrangement of the first string is a substring of a second string.

3.1 Solution: + sliding window histogram

Analyzing string s2contains the string s1arrayed string s1all the elements can be randomly combination. By comparing s1the length of the s2substring with a s1various number of characters to be judged whether or not the same (the same number as long as the various elements s1can be combined into a current s2form substring).

When sliding, the s2current character after the window width is added to the statistics inside the array (count by one), the first character in the left of the window which results remove statistical array (count by one).

Example 1:

输入:s1 = "ab" s2 = "eidbaooo"
输出:true
解释:s2 包含 s1 的排列之一 ("ba").

Example 2:

输入:s1= "ab" s2 = "eidboaoo"
输出:false

note:

  1. Input string contains only lowercase letters. (A total of 26 lowercase letters.)
  2. The length of the two strings are in [1, 10,000]between.
bool checkInclusion(char * s1, char * s2)
{
	char *ps1 = s1;
	char *ps2 = s2;
	int len_s1 = 0;
	int len_s2 = 0;
	int head = 0;
	int tail = 0;
	int idx = 0;
	int histogram_num = 26;
	int histogram_s1[26] = { 0 };
	int histogram_sub[26] =	{ 0 };

	if ((NULL == s1) || (NULL == s2))
	{
		return false;
	}

	len_s1 = strlen(s1);
	len_s2 = strlen(s2);

	if (len_s1 > len_s2)
	{
		return false;
	}

	// histogram data
	for (idx = 0; idx < len_s1; idx++)
	{
		histogram_s1[ps1[idx] - 'a']++;
		histogram_sub[ps2[idx] - 'a']++;
	}

	for (head = 0, tail = len_s1; tail < len_s2; head++, tail++)
	{
		for (idx = 0; idx < histogram_num; idx++)
		{
			if (histogram_s1[idx] != histogram_sub[idx])
			{
				break;
			}
		}

		if (idx == histogram_num)
		{
			return true;
		}

		histogram_sub[ps2[head] - 'a']--;
		histogram_sub[ps2[tail] - 'a']++;
	}

	for (idx = 0; idx < histogram_num; idx++)
	{
		if (histogram_s1[idx] != histogram_sub[idx])
		{
			break;
		}
	}

	if (idx == histogram_num)
	{
		return true;
	}
	else
	{
		return false;
	}
}

Here Insert Picture Description

Here Insert Picture Description

3.1 Special test case

bool checkInclusion(char * s1, char * s2)
{
	char *ps1 = s1;
	char *ps2 = s2;
	int len_s1 = 0;
	int len_s2 = 0;
	int head = 0;
	int tail = 0;
	int idx = 0;
	int histogram_num = 26;
	int histogram_s1[26] = { 0 };
	int histogram_sub[26] =	{ 0 };

	if ((NULL == s1) || (NULL == s2))
	{
		return false;
	}

	len_s1 = strlen(s1);
	len_s2 = strlen(s2);

    printf("len_s1 = %d\n", len_s1);
    printf("len_s2 = %d\n", len_s2);

	if (len_s1 > len_s2)
	{
        printf("s1 = \"\*\*\*\", s2 = \"\";");
		return false;
	}

	// histogram data
	for (idx = 0; idx < len_s1; idx++)
	{
		histogram_s1[ps1[idx] - 'a']++;
		histogram_sub[ps2[idx] - 'a']++;
	}

	for (head = 0, tail = len_s1; tail < len_s2; head++, tail++)
	{
		for (idx = 0; idx < histogram_num; idx++)
		{
			if (histogram_s1[idx] != histogram_sub[idx])
			{
				break;
			}
		}

		if (idx == histogram_num)
		{
            printf("s1 = \"\", s2 = \"\*\*\*\";");
			return true;
		}

		histogram_sub[ps2[head] - 'a']--;
		histogram_sub[ps2[tail] - 'a']++;
	}

	for (idx = 0; idx < histogram_num; idx++)
	{
		if (histogram_s1[idx] != histogram_sub[idx])
		{
			break;
		}
	}

	if (idx == histogram_num)
	{
        printf("s1 = \"\", s2 = \"\";");
		return true;
	}
	else
	{
		return false;
	}
}

3.1.1 Test Case - s1 = "yong", s2 = "";

Here Insert Picture Description

Here Insert Picture Description

3.1.2 Test Case - s1 = "", s2 = "qiang";

Here Insert Picture Description

Here Insert Picture Description

3.1.3 Test Case - s1 = "", s2 = "";

Here Insert Picture Description

Here Insert Picture Description

Published 523 original articles · won praise 1850 · Views 1.12 million +

Guess you like

Origin blog.csdn.net/chengyq116/article/details/104946235