ACM template
describe
Answer key
Ah, the second time hit CF was the second card dead, not difficult problem, is really poor English, ignored (because did not understand) a very important condition, the results think difficult.
Given a string, no matter how it is divided, the four adjacent letters are not the same. We need to know what the letters are, but we don’t need to know the exact position of the letters. We need to know how many of the letters are needed to make the difference between ‘! ‘And I’m done.
At first, I misunderstood the last sentence of the second line, thinking that at least one out of every four lights was on, so I wondered if there was something wrong with the sample. Later, I directly ignored the sentence, because I did not understand it. And then, I started doing this really clunky thing, matching letters, filling letters, thinking about a lot of things that didn’t make sense.
Then, I suddenly realized that this problem is not a problem of violent matching and substitution, but a problem of finding rules. Therefore, I re-examined the last sentence of the second paragraph and understood that the general idea is that each letter appears at least once… So this is a straightforward rule to solve for, if you want to make sure that every four adjacent letters are different, then the i-th letter is definitely the same as the i-4 and I +4 letters, if they exist. So, it’s the same violent solution, just violent substitution, and you don’t have to worry too much about matching, because you’ve been thinking a lot about matching, and you’ve ignored the rules.
Of course, I have to say that my problems at the beginning were not only because I did not understand the meaning of the question, but also because I did not calm down to observe the general law of the question. When I knew the law, I did not need to consider the matching problem at all, and could simply replace it with violence.
Alas, I remained one of the world 鈥 檚 “remaining” buoyed by a simple question that got me nowhere and kept me informed
code
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 111;
char S[MAXN];
int kr, kb, ky, kg;
void init(a)
{
kr = kb = ky = kg = 0;
}
int main(int argc, const char * argv[])
{
while (cin >> S)
{
init(a);int len = (int)strlen(S);
while (true)
{
int flag = 0;
for (int i = 0; i < len; i++)
{
if (S[i] == '! ')
{
flag = 1;
if (i > 3 && S[i - 4] != '! ')
{
S[i] = S[i - 4];
switch (S[i - 4])
{
case 'R':
kr++;
break;
case 'B':
kb++;
break;
case 'Y':
ky++;
break;
case 'G':
kg++;
break; }}else if (i < len - 4 && S[i + 4] != '! ')
{
S[i] = S[i + 4];
switch (S[i + 4])
{
case 'R':
kr++;
break;
case 'B':
kb++;
break;
case 'Y':
ky++;
break;
case 'G':
kg++;
break; }}}}if(! flag) {break; }}printf("%d %d %d %d\n", kr, kb, ky, kg);
}
return 0;
}
Copy the code