java - 代码厨师 : Holes in the Text

标签 java

我正在研究以下CodeChef question :

Chef wrote some text on a piece of paper and now he wants to know how many holes are in the text. What is a hole? If you think of the paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For example letters "A", "D", "O", "P", "R" divide the plane into two regions so we say these letters each have one hole. Similarly, letter "B" has two holes and letters such as "C", "E", "F", "K" have no holes. We say that the number of holes in the text is equal to the total number of holes in the letters of the text. Help Chef to determine how many holes are in the text.

Input

The first line contains a single integer T <= 40, the number of test cases. T test cases follow. The only line of each test case contains a non-empty text composed only of uppercase letters of English alphabet. The length of the text is less then 100. There are no any spaces in the input.

Output

For each test case, output a single line containing the number of holes in the corresponding text.

Example

Input:
2
CODECHEF
DRINKEATCODE

Output:
2
5

我正在提交以下代码,但法官一直显示错误答案(WA)。有什么建议吗?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class test {
    static short[] srr=new short[41];

    public static short getHoles(String st){
        short holes=0;
        for (int i = 0; i < st.length(); i++) {
            char c= st.charAt(i);
            if(c=='A') holes++;

            if(c=='D') holes++;

            if(c=='P') holes++;

            if(c=='O') holes++;

            if(c=='R') holes++;

            if(c=='Q') holes++;

            if(c=='B'){
                holes++;
                holes++;
            }
        }

        return holes;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        short cases;
        String stt= s.readLine();
        cases=Short.parseShort(stt);
        String temp;
        short t;
        for (int i = 0; i < cases; i++) {
            temp=s.readLine();
            t = getHoles(temp);
            srr[i]=t;
        }

        for (int i = 0; i< 45; i++) {
            if(srr[i]==0) break;
            System.out.println(srr[i]); 
        }

    }
}

最佳答案

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class Letter {
    
        public static void main(String[] args) throws NumberFormatException, IOException {
            // TODO Auto-generated method stub
            InputStreamReader is= new InputStreamReader(System.in);
            BufferedReader br= new BufferedReader(is);
            int n= Integer.parseInt(br.readLine());
            for(int i=0;i<n;i++) {
                int holes=0;
                String s= br.readLine();
                char[] ch= s.toCharArray();
                for(int j=0;j<ch.length;j++) {
                    
                    if(ch[j]=='A'||ch[j]=='D'||ch[j]=='O'||ch[j]=='Q'||ch[j]=='P'||ch[j]=='R') {
                        holes= holes+1;
                    }
                    else if(ch[j]=='B') {
                        holes=holes+2;  
                    }
                    else {
                        holes=holes+0;
                    }
                }
            System.out.println(holes);  
            }
            }
    }

关于java - 代码厨师 : Holes in the Text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34745395/

相关文章:

java - 查找特定的异常和相应的消息

java - 如何使用Java在YouTube数据API v3中设置nextPageToken?

java - MVC 和 Spring MVC 单元测试

Java打印固定宽度的字符串

java - 结果集不迭代指定列的值

java - FileNotFoundException、URL 连接错误、Babelfy、Java

java - 什么是时间复杂度 O(1) 次,Java 中的 O(n) 次

java - 将 Character[] 的范围转换为 String

java - 如何从 url 创建 FileHostObject?

java - 如何实现椭圆GradientPaint?