1040(15)

本文介绍了一种在字符串中寻找并计数特定模式PAT的算法实现。通过对字符数组的遍历,算法统计每个'A'字符前的'P'和其后的'T'的数量,以此计算所有可能的PAT组合数。最终结果需对1000000007取模。

思路:对A进行遍历,然后遍历A前面有a个P,A后面有b个T,那么就有a*b个PAT,这样的A有c个的话,总数就是a*b*c,三个超时,看来需要优化

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
    // write your code here
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        char[] input=str.toCharArray();
        int i,j,k,l,m1=0,m2=0,m3=0,n;
        int[] c1=new int[100000];
        int[] c2=new int[100000];
        int[] c3=new int[100000];
        for (l=0;l<100000;l++){
            c1[l]=0;
            c2[l]=0;
            c3[l]=0;
        }
        for (i=0;i<input.length;i++){
            if (input[i]=='A'&&i!=0){
                c1[i]++;
                for (j=0;j<i;j++){
                    if (input[j]=='P'){
                        c2[i]++;
                    }
                }
                for (k=i+1;k<input.length;k++){
                    if (input[k]=='T'){
                        c3[i]++;
                    }
                }
            }
        }
        long sum=0;
        for (n=0;n<=input.length;n++){
            sum=sum+c1[n]*c2[n]*c3[n];
        }
        System.out.println(sum%1000000007);
    }
}

 

转载于:https://www.cnblogs.com/zhuzehua/p/9835739.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值