Codeforces 758B:Blown Garland(模拟)

http://codeforces.com/problemset/problem/758/B

题意:给出一个字符串,每4个位置对应一个颜色,如果为‘!’的话,代表该灯泡是坏的,问最后每个颜色坏的灯泡的数目。

思路:题意实在挺难懂的(可能我到现在还没看懂)。看样例看了好久才看出来。例如最后一个样例“!GB!RG!Y!”:可以拆分成"!GB!”,"RG!Y","!",R只在第一个位置出现,说明R是在第一个位置的,以此类推,G是在第二个位置,B是在第三个位置,Y是在第四个位置。那么对应每个位置出现的“!”的数目,就是要求的对应颜色的值了。

首先先枚举一次串通过i%4找到每个颜色对应的位置,然后再枚举一遍串,如果对应位置出现“!”就对应颜色答案+1.

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <string>
 6 #include <cmath>
 7 #include <queue>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <stack>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 #define N 100010
15 typedef long long LL;
16 char s[110];
17 int a[10], id[10];
18 
19 int main() {
20     cin >> s;
21     int len = strlen(s);
22     memset(id, -1, sizeof(id));
23     for(int i = 0; i < len; i++) {
24         if(s[i] == 'R') id[0] = i % 4;
25         if(s[i] == 'B') id[1] = i % 4;
26         if(s[i] == 'Y') id[2] = i % 4;
27         if(s[i] == 'G') id[3] = i % 4;
28     }
29     for(int i = 0; i < 4; i++) {
30         if(id[i] == -1) {
31             id[i] = i;
32         }
33     }
34     for(int i = 0; i < len; i++) {
35         if(i % 4 == id[0] && s[i] != 'R') a[0]++;
36         if(i % 4 == id[1] && s[i] != 'B') a[1]++;
37         if(i % 4 == id[2] && s[i] != 'Y') a[2]++;
38         if(i % 4 == id[3] && s[i] != 'G') a[3]++;
39     }
40     for(int i = 0; i < 4; i++) printf("%d ", a[i]);
41     return 0;
42 }
原文地址:https://www.cnblogs.com/fightfordream/p/6321833.html