zoukankan      html  css  js  c++  java
  • PAT甲级——1093 Count PAT's (逻辑类型的题目)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93389073

    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 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的组合数量,字符配对要遵循原始字符串的先后顺序。

    思路:开三个数组PPos、APos、TPos分别记录'P'、'A'、'T'三个字符所在的位置。遍历APos,对于当前位置的'A',在它之前的'P'的个数和在它之后的'T'的个数之积就是当前的'A'的组合数量,所有的'A'的组合数量之和就是答案。

     1 #include <iostream>
     2 #include <string>
     3 #include <vector>
     4 using namespace std;
     5  
     6 vector<int> PPos, APos, TPos;
     7 string s;
     8  
     9 int main()
    10 {
    11     int num = 0;
    12     cin >> s;
    13     for (int i = 0; i < s.length(); i++) {
    14         if (s[i] == 'P')
    15             PPos.push_back(i);
    16         if (s[i] == 'A')
    17             APos.push_back(i);
    18         if (s[i] == 'T')
    19             TPos.push_back(i);
    20     }
    21     int PSize = PPos.size(),
    22         ASize = APos.size(),
    23         TSize = TPos.size(),
    24         PIndex = 0,
    25         TIndex = 0;
    26     for (int i = 0; i < ASize; i++) {
    27         while (PIndex < PSize && PPos[PIndex] < APos[i]) {
    28             PIndex++;
    29         }
    30         while (TIndex < TSize && TPos[TIndex] < APos[i]) {
    31             TIndex++;
    32         }
    33         if (TIndex >= TSize)
    34             break;
    35         num += PIndex * (TSize - TIndex);
    36         num = num % 1000000007;
    37     }
    38     cout << num << endl;
    39     return 0;
    40 }
  • 相关阅读:
    2429: [HAOI2006]聪明的猴子
    1789: [Ahoi2008]Necklace Y型项链
    3399: [Usaco2009 Mar]Sand Castle城堡
    3713: [PA2014]Iloczyn
    1907: 树的路径覆盖
    2751: [HAOI2012]容易题(easy)
    算法模板——计算几何2(二维凸包——Andrew算法)
    算法模板——splay区间反转 2
    算法模板——Dinic网络最大流 2
    算法模板——Dinic最小费用最大流
  • 原文地址:https://www.cnblogs.com/yinhao-ing/p/11073418.html
Copyright © 2011-2022 走看看