美文网首页PAT
甲级| 1093.Count PAT's

甲级| 1093.Count PAT's

作者: yzbkaka | 来源:发表于2019-08-09 12:13 被阅读3次

    题目描述

    The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.

    Now given any string, you are supposed to tell the number of PAT's contained in the string.

    输入描述

    Each input file contains one test case. For each case, there is only one line giving a string of no more than 10​5​​ characters containing only P, A, or T.

    输出描述

    For each test case, print in one line the number of PAT's contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.

    输入例子

    APPAPT

    输出例子

    2

    我的代码

    #include<stdio.h>
    #include<string.h>
    const int mod=1000000007;
    int main(){
        char a[100001];
        int p[100001]={0};
        gets(a);
        int len=strlen(a);
        for(int i=0;i<len;i++){
            if(i>0){
                p[i]=p[i-1];
            }
            if(a[i]=='P'){
                p[i]++;
            }
        }
        int t=0,ans=0;
        for(int i=len-1;i>=0;i--){
            if(a[i]=='T'){
                t++;
            }
            else if(a[i]=='A'){
                ans=(ans+p[i]*t)%mod;
            }
        }
        printf("%d",ans);
        return 0;
    } 
    

    相关文章

      网友评论

        本文标题:甲级| 1093.Count PAT's

        本文链接:https://www.haomeiwen.com/subject/oowcjctx.html