1345: QAQ

1345: QAQ

时间限制: 1 Sec  内存限制: 256 MB

题目描述

给出一个字符串,让你找到其中一共有多少个子序列"QAQ"。子序列"QAQ"可以不连续,但是字母顺序必须是准确的。

输入

第一行:一个整数T,表示测试实例个数。

对于每组测试实例:包含一个长度为 n (1 ≤ n ≤ 100)的字符串。

输出

每组测试实例输出一行:包含一个整数,子序列"QAQ"的个数。

样例输入

2
QAQAQYSYIOIWIN
QAQQQZZYNOIWIN

样例输出

4
3

提示

如第一组样例:共有4个子序列"QAQ",分别如下:

 "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN".

import java.util.Scanner;
 
public class Main {
    private static int t;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
         
        int n = sc.nextInt();
        String s[] = new String[n];
         
        for (int i = 0; i < n; i++) {
            s[i] = sc.next();
        }
        for (int i = 0; i < s.length; i++) {
            f(s[i],1);
            System.out.println(t);
            t = 0;
        }
    }
    private static void f(String s, int j) {
        if(j==1||j==3) {
            for (int i = 0; i < s.length(); i++) {
                if(s.charAt(i) =='Q') {
                    f(s.substring(i),j+1);
                }
            }
        }else if(j==2) {
            for (int i = 0; i < s.length(); i++) {
                if(s.charAt(i) =='A') {
                    f(s.substring(i),j+1);
                }
            }
        }else if(j==4) {
            t++;
        }
    }
}
/**************************************************************
    Problem: 1345
    User: 20161514325
    Language: Java
    Result: 正确
    Time:334 ms
    Memory:39948 kb
****************************************************************/

猜你喜欢

转载自blog.csdn.net/qq_39507723/article/details/83720692