zoukankan      html  css  js  c++  java
  • 1093 Count PAT's

    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 1 characters containing only PA, 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”。

    思路:

    刚开始我想的是从后往前扫描,找到最后的AT所在的位置,然后在统计AT前面有多少个P。现在看这样想真的很傻,但是,当我这样想过之后,提交发现不对的时候,真的很难发现这样为什错误,之所以这样想可能是受样例的影响吧。

    正确的做法应该是从前向后扫描字串中‘P’的个数,然后再从后向前扫描‘T’的个数,最后扫描‘A’,用‘A’前面‘P’的个数与后面‘T’的个数相乘,再求和。

    Code:

    #include<iostream>
    #include<string>
    #include<vector>
    
    using namespace std;
    
    int main() {
        string str;
        cin >> str;
    
        int len = str.length();
        vector<int> numOfP(len, 0);
        vector<int> numOfT(len, 0);
        
        int countP = 0;
        int countT = 0;
        int countPAT = 0;
        for (int i = 0; i < len; ++i)
            if (str[i] == 'P') numOfP[i] = ++countP;
            else numOfP[i] = countP;
        for (int i = len-1; i >= 0; --i)
            if (str[i] == 'T') numOfT[i] = ++countT;
            else numOfT[i] = countT;
        for (int i = 0; i < len; ++i)
            if (str[i] == 'A')
                countPAT = (countPAT + numOfP[i] * numOfT[i]) % 1000000007;
    
        cout << countPAT << endl;
        return 0;
    }
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    Oracle 安装及其遇到的问题
    集合与Iterator
    Java 基本数据类型长度
    TextFile 类的创写
    Base64编码通过URL传值的问题
    HttpUrlConnection访问Servlet进行数据传输
    Servlet 的认识
    高聚合低耦合
    Exception loading sessions from persistent storage 这个问题的解决
    ARTS打卡计划第六周
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12617960.html
Copyright © 2011-2022 走看看