目录

P8700-蓝桥杯-2019-国-B-解谜游戏-string与cstringmemset介绍

P8700 [蓝桥杯 2019 国 B] 解谜游戏–string与cstring、memset()介绍

题目

https://i-blog.csdnimg.cn/direct/81353ce63be9487cbd40cbce8fd78861.png

解析

https://i-blog.csdnimg.cn/direct/5a9cd753fdf34376bab862b3a3d421b2.png

借大佬图用用,内中外圈的数量比是1:2:3,所以无论怎么旋转在内圈的一个位置都对应着中圈的两个位置以及外圈的三个位置(这个是固定的)

所以需要我们做的就是判断这几个位置上的绿:红:黄=3:2:1。然后用哈希法记录

从下述的代码中我们可以看见,每次循环完有个重置哈希表的步骤,这里要用到一个函数memset()【 #include <cstring>下的一个函数

memset()的主要作用:

i.初始化内存为0(清零):

int arr;
memset(arr, 0, sizeof(arr)); // 将 arr 所有元素初始化为0

ii.设置字符数组的值:

char buffer;
memset(buffer, 'A', 10); // 前10字节设为字符'A'

iii.初始化结构体:

struct Data {
    int id;
    char name;
};
Data data;
memset(&data, 0, sizeof(Data)); // 清零整个结构体

代码

#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <algorithm>
#include <math.h>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
using namespace std;
int t;
string s1, s2, s3;
int flag, q[100];

int main() {
	cin >> t;
	for (int i = 0; i < t; i++) {
		cin >> s1 >> s2 >> s3;
		for (int j = 0; j < 4; j++) {
			q[s3[j]]++, q[s2[j]]++;
			q[s2[j + 4]]++;
			q[s1[j]]++, q[s1[j + 4]]++, q[s1[j + 8]]++;
			if (q['G'] != 3 || q['R'] != 2 || q['Y'] != 1) {
				cout << "NO" << endl;
				flag = 1;
				memset(q, 0, sizeof q);
				break;
			}
			memset(q, 0, sizeof q);
		}
		if (!flag) {
			cout << "YES" << endl;
		}
		flag = 0;
	}
	return 0;
}