[HDU] [3788] ZOJ Problem

Problem Description

Loading...

Problem Analysis

A pattern-finding problem
First analyze the conditions for AC

  1. ZOJ can be AC; this is simple
  2. xzojx can be AC, that is, the count before z equals the count after j, with one between them
  3. If azbjc can be AC, then azbojac can also be AC
    The third one has an “if”
    That is, azbojac being AC has a precondition
    That is, strings of this form can be derived step by step

Analyze the conditions
If condition 3 is satisfied, initially we must have a=c, b=1
Then each time we can derive recursively
the counts of the three o’s
a 2 a+c
a 3 2a+c
…
Because the precondition is a=c
Therefore
the three counts must satisfy
a b b*a
That is, the product of the count before Z and the count between Z and J must equal the count after J
So we can rule out other completely impossible cases
(no o between z and j; missing z or j)
Write the AC condition

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
#include<string.h>
int main() {
	char s[1200];
	while(gets(s)!=NULL) {
		int l=strlen(s);
		int a=0,b=0,c=0,z=0,j=0;
		bool AC=false;
		for(int i=0; i<l; i++) {
			if(s[i]=='z') {
				if(z==0)
					z++;
				else {
					break;
				}
			} else if(s[i]=='j') {
				if(j==0)
					j++;
				else {
					break;
				}
			} else {
				if(z==0)
					a++;
				else if(j==0)
					b++;
				else
					c++;
			}
		}
		if(b>0&&a*b==c)
			AC=true;
		if(!z||!j)
			AC=false;
		if(AC)
			printf("Accepted\n");
		else
			printf("Wrong Answer\n");
	}
	return 0;
}

Problem link: HDU 3788 ZOJ Problem

Loading...