Zoj can ac

Source: Internet
Author: User
Description:
Determine whether a given string (containing only 'Z', 'O', and 'J') can be AC.

The AC rules are as follows:
1. zoj can communicate with each other;
2. If the string format is xzojx, it can also be AC, where X can be n 'O' or is empty;
3. If azbjc can be AC, azbojac can also be AC, where A, B, and C are n 'O' or empty;
Input:
The input contains multiple groups of test cases. Each line has a string that only contains 'Z', 'O', and 'J'. The length of the string is less than or equal to 1000.
Output:
For a given string, if the string can be AC, please output the string "accepted"; otherwise, please output "Wrong answer ".
Sample input:
zojozojoozoojoooozoojoooozoojozojooooozojozojoooo
Sample output:
AcceptedAcceptedAcceptedAcceptedAcceptedAcceptedWrong AnswerWrong Answer
Source:

A true Study on computer and software engineering of Zhejiang University in 2010

Analysis:

AC: a o + Z + B O + J + c o

C = a * B, and B> = 1

(O ^ A) Z (o ^ B) J (o ^ c) C = a * B, and B> = 1

Z and J all have only one. And J appears after Z,B> = 1 can ensure that J appears after Z.

import java.util.*;public class Main { public static boolean check(String s)    {        int p=0,q=0,zp,jp,a,b,c;        p=s.indexOf("z");        q=s.lastIndexOf("z");                 if(p!=q)        return false;        if(p==-1)            return false;        zp=p;                 p=s.indexOf("j");        q=s.lastIndexOf("j");        if(p!=q)        return false;        if(p==-1)            return false;        jp=p;                 a=zp;        b=jp-a-1;        c=s.length()-jp-1;        if((c==a*b) && (b>=1))            return true;                 return false;       }    public static void main(String[] args) {        // TODO Auto-generated method stub                 String t;        boolean b;                         Scanner scanneer = new  Scanner(System.in);        while (scanneer.hasNext() )        {            t=scanneer.nextLine();            b=check(t);                         if(b)                System.out.println("Accepted");            else                System.out.println("Wrong Answer");                        }             }}/**************************************************************    Problem: 1006    User: caiyunfree20    Language: Java    Result: Accepted    Time:930 ms    Memory:27016 kb****************************************************************/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.