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;
    }
    
  • 相关阅读:
    Lc112_路径总和
    多线程(4)
    Lc257_二叉树的所有路径
    Lc226_翻转二叉树
    Lc145_二叉树的后序遍历
    Lc144_二叉树的前序遍历
    Lc102_二叉树的层序遍历
    Lc101_对称二叉树
    Lc100_相同的树
    Lc94_二叉树的中序遍历
  • 原文地址:https://www.cnblogs.com/moonlight1999/p/15758590.html
Copyright © 2011-2022 走看看