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
    
    遇到p计算p的数量,遇到a计算a的数量,遇到一个t就算一下t前面有多少个pa,而计算pa就是每个a之前有几个p,所以遇到不是a的(p或t),先看一下a是否为0,如果不是,就pa += p * a,pa的数量增加,然后a归零。
    代码:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    int main()
    {
        int pa = 0,pat = 0,p = 0,a = 0;
        char s[100005];
        cin>>s;
        for(int i = 0;s[i];i ++)
        {
            if(s[i] == 'P')
            {
                if(a)
                {
                    pa += p * a;
                    a = 0;
                }
                p ++;
            }
            else if(s[i] == 'A')a ++;
            else
            {
                if(a)
                {
                    pa += p * a;
                    a = 0;
                }
                pat = (pat + pa) % 1000000007;
            }
        }
        cout<<pat;
    }

  • 相关阅读:
    学习进度(2)
    模拟退火 [JSOI2004]平衡点 / 吊打XXX
    快读快写 O3 优化
    卡特兰数(Catalan)公式、证明、代码、典例
    树状数组 :单点修改,区间查询
    倍增 [模板]最近公共祖先LCA
    对测 【模拟】
    对测 【离线DP+二分】
    模拟退火 (骗分算法)
    基础数论入门
  • 原文地址:https://www.cnblogs.com/8023spz/p/8394896.html
Copyright © 2011-2022 走看看