zoukankan      html  css  js  c++  java
  • 1093 Count PAT's (25 分)(数学问题)

    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.

    Input Specification:

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

    Output Specification:

    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.

    Sample Input:

    APPAPT

    Sample Output:

    2

    分析:

    要想知道构成多少个PAT,那么遍历字符串后对于每一A,它前面的P的个数和它后面的T的个数的乘积就是能构成的PAT的个数。然后把对于每一个A的结果相加即可辣么就简单啦,只需要先遍历字符串数一数有多少个T然后每遇到一个T呢~countt–;每遇到一个P呢,countp++;然后一遇到字母A呢就countt * countp把这个结果累加到result中最后输出结果就好啦~对了别忘记要对10000000007取余哦~

    为什么要对1000000007取模?
    原文链接:https://blog.csdn.net/liuchuo/article/details/51985909

    题解

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        string s;
        cin>>s;
        int n=0,cntp=0,cnta=0,cntt=0;
        for(int i=0;i<s.size();i++){
            if(s[i]=='T') cntt++;
        }
        for(int i=0;i<s.size();i++){
            if(s[i]=='P') cntp++;
            if(s[i]=='A'){
    			//注意这句的正确写法
                n=(n+cntp*cntt%1000000007)%1000000007;
            }
            if(s[i]=='T') cntt--;
        }
        cout<<n;
        return 0;
    }
    
  • 相关阅读:
    Module:template
    Grunt:GruntFile.js
    Grunt:常见错误
    架构:架构-1
    架构:目录
    DS:template
    Jasper:用户指南 / 设备 / 生命周期管理 / SIM 卡状态
    Jasper-Api:接口测试
    linux服务之git
    Java实现 洛谷 P1487 陶陶摘苹果(升级版)
  • 原文地址:https://www.cnblogs.com/moonlight1999/p/15758590.html
Copyright © 2011-2022 走看看