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

    1093. Count PAT's (25)

    时间限制
    120 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CAO, Peng

    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

    思路

    试了下直接暴搜,果然最后几个测试点超时,看来还是得用dp。
    这道题DP的思路就是用两个int数组P[n],T[n]保存第i个字符的左边字母'P'的个数P[i]和右边字母'T'的个数T[i],当遍历到的字符是'A'时,那么以这个A为基础的"PAT"字符串个数就是它左侧P字母个数乘以右侧T字母个数。即P[i]*T[i]

    代码
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
        string s;
        while(cin >> s)
        {
            vector<int> P(100000,0);
            vector<int> T(100000,0);
            for(int i = 1,j = s.size() - 2;i < s.size() && j >= 0;i++,j--)
            {
                if(s[i - 1] == 'P')
                {
                    P[i]++;
                }
                if(s[j + 1] == 'T')
                {
                    T[j]++;
                }
                P[i] += P[i - 1];
                T[j] += T[j + 1];
            }
    
            int sum = 0;
            for(int i = 0;i < s.size();i++)
            {
                if(s[i] == 'A')
                {
                    sum += P[i] * T[i];
                    sum %= 1000000007;
                }
    
            }
            cout << sum << endl;
        }
    }
  • 相关阅读:
    印刷行业合版BOM全阶维护示例
    C#实现WinForm禁止最大化、最小化、双击标题栏、双击图标等操作的方法
    EasyUI Tree节点拖动到指定容器
    Excel GET.DOCUMENT说明
    Excel GET.CELL说明
    ExecuteExcel4Macro (宏函数)使用说明
    MSSQL:查看所有触发器信息的命令
    SQL Server 2008作业失败:无法确定所有者是否有服务器访问权限
    Cocoa History
    Working with Methods
  • 原文地址:https://www.cnblogs.com/0kk470/p/7647050.html
Copyright © 2011-2022 走看看