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 }
  • 相关阅读:
    进程实际操作篇2
    进程的实际操作篇1
    进程的理论知识
    解决套接字粘包,udp套接字对象的使用和socketserver模块实现并发
    day24-网络知识扫盲,socket的基本使用
    day23-网络编程之互联网基础,tcp/ip协议详细介绍
    day21-多态和多态性,鸭子类型,反射,内置方法,异常处理
    JAVA WEB小测
    JAVA动手动脑
    JAVA课上动手动脑问题2
  • 原文地址:https://www.cnblogs.com/yinhao-ing/p/11073418.html
Copyright © 2011-2022 走看看