0 and 1 C language

describe

        It is very fun to study the 01 string. Now there is a 01 string of length n. When the number of 0 and 1 in a continuous substring is the same, this substring is a good substring. Now please write the code Figure out how many good substrings are in this 01 string of length n.

enter

Enter T (1<=T<=10) groups of data, enter an integer n (1<=n<=1000) in the first line of each group, representing the length of the 01 string, and then enter a line of 01 string with a length of n.

output

Output T lines, each with an integer as the answer.

sample input

2

2

01

4

0101

Sample output

1

4

Ideas and Analysis:

        First of all, we must understand the description of the title, what is a good substring in the 01 string. According to the definition of the title, such as 010101, its division should be like this:

        According to this division, the most intuitive finding is the length to be judged, that is, its end position is not certain, so it is necessary to control the length of each substring that needs to be judged through variables. At the same time, it is also necessary to judge the starting position. We use pointers to describe where the judgment starts and ends. Define two pointer variables *start and *end. As shown in the figure:

We need to consider the following aspects.        

 1. How to get characters 0 and 1?

        From the above animation, every time *start is moved, *end is moved once, and it ends when *end encounters the character '\0'.

2. How to truncate the length?

        The above case is for substrings of two lengths. According to the definition of a good substring, we need the length of each truncation should be an even multiple, which is more reasonable. For example, 0101, the first truncation of two lengths, according to the above animation, is 01, 10, 01. The second truncation is 0101 when we are in the case of a substring of 4 lengths. If the truncation length is increased by 1 after each cycle, such as 0101, the first truncation length is 2, and the second truncation length is 3, which is 010, which definitely does not meet the definition of a good string. After this truncation, in returning 1, it is fine.

3. How many times do you need to cycle?

        If we look for this pattern, such as 01010101:

         When the length is 8, the length of 2 requires 7 small loops. A length of 4 requires 5 small loops. A length of 6 requires 3 small loops. A length of 8 requires 1 small loop. The whole process requires four large loops to change the truncation length from 2 to 8. In this way, the judgment of the whole situation can be completed. Of course, you can try a few more 01 strings.

        Through the above analysis, we know that the whole process requires 4 looping statements (1 is the multiple sets of input required by the question). From the inside to the outside, you need to loop how to move and how to truncate the length. There are several such truncation length loops, and you need to cycle through different truncation lengths several times, and there are several sets of inputs. Conversely, taking 01010101 as an example, 01010101. A group of input 01010101 needs to be divided into 4 different lengths as shown in the above picture. Among them, the length of 2 needs 7 cycles to find all 2 lengths. Let it judge the value inside, and after judging 0, it needs to move once to judge 1.

        Such an analysis is the most intuitive. The implementation code is divided into pointer form (implemented by me), and array form (implemented by a friend of mine @Idyllic930, there are changes.), it should be noted that the implementation method is not unique, you can use for or while.

//指针形式
#include<stdio.h>

int main(void)
{
	int t = 0;
	scanf("%d", &t);
	int n = 0;
	char arr[1000] = { 0 };

	for (int m = 0; m < t; m++)//题目要求的循环输入
	{
		int i = 0;//总的循环的次数
		int j = 1;//start要回退的值
		int k = 1;//end要截断的长度
		scanf("%d", &n);
		scanf("%s", arr);

		int count_zero = 0;//计数0的个数
		int count_one = 0;//计数1的个数
		int count = 0;//计数01中好子串的个数

		char* start = arr;//start的位置
		char* end = arr + k;//end的位置

		while (i < n / 2)//循环次数
		{
			while (*end != '\0')
			{
				while(start <= end && *end != '\0')//判断01串里面的值;
				{
					if (*start == '1')//这个值为1,计数1的变量的值加1;
					{
						count_one++;
					}
					if (*start == '0')//这个值为0,计数0的变量的值加1;
					{
						count_zero++;
					}
					start++;//让start指向下一位
				}
				if (count_zero == count_one)//计数1和计数0是否相等,若相等,计数01中好串的变量加1;
				{
					count++;
				}
				end++;//end指向下一位;
				start -= j;//star退回到他-j位;
				//初始化
				count_zero = 0;
				count_one = 0;
			}
			j += 2;
			i += 1;
			k += 2;
			end = arr + k;
			start = arr;
		}
		printf("%d\n", count);
	}
	return 0;
}

The result is as follows:

#include<stdio.h>
int main()
{
	int t, i;
	scanf("%d", &t);
	for (i = 0; i < t; i++)
	{
		int j = 0;
		int k = 0;
		int count_one = 0;
		int count_zero = 0;
		int count = 0;

		int len = 0;
		int index = 0;
		char arr[100] = { 0 };

		scanf("%d", &len);
		scanf("%s", arr);

		for (j = 1; j < len;)
		{
			for (k = 0; k < len - 1; k++)
			{
				for (index = k, count_zero = 0, count_one = 0; index < (k + 2 * j); index++)
				{
					if (arr[index] == '1')
					{
						count_one++;
					}	
					else if (arr[index] == '0')
					{
						count_zero++;
					}
				}
				if (index > len)
				{
					break;
				}	
				if (count_one == count_zero)
				{
					count++;
				}
					
			}
			j++;
		}
		printf("%d\n", count);
	}
	return 0;
}

The result is as follows:

Guess you like

Origin blog.csdn.net/Naion/article/details/121681401