PAT Advanced level 1093

1093 Count PAT‘s (25)(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 10^5^ 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

这题不好AC啊

/**********************
author: yomi
date: 18.6.17
ps:
不会不会 虽然乙级刷过这道题 但是现在还是不会 要多多体会这种思想
我能想到的就只有暴力循环 T^T
int len = strlen(str);
循环中一定要利用事先计算好的len
**********************/
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
using namespace std;
const int mod = 1000000007;
int plnum[100010];
char str[100010];
int main()
{
    memset(str, 0, sizeof(str));
    memset(plnum, 0, sizeof(plnum));
    scanf("%s", str);
    int len = strlen(str);
    ///计算每一个位置的左边有多少个p,并存在数组中
    for(int i=0; i<len; i++){///如果此处不用len,而是随时计算的话,会超时
        if(i > 0){
            plnum[i] = plnum[i-1];
        }
        if(str[i] == ‘P‘){
            plnum[i]++;
        }
    }
    int ans = 0, trnum = 0;
    for(int i=len-1; i>=0; i--){
        if(str[i] == ‘T‘){
            trnum++;
        }
        else if(str[i] == ‘A‘){
            ans = (ans+plnum[i]*trnum)%mod;
        }

    }
    printf("%d\n", ans);
    return 0;
}

/***
明显下面的解法更好

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    cin >> str;
    int num_t=0, num_at=0, num_pat=0;
    ///cout << str.length() << endl;
    for(int i=str.size()-1; i>=0; i-- ){///天呐 我这就写了个size_t, 就死循环了
        if(str[i] == ‘T‘){
            num_t ++;
            ///num_t %= 1000000007;
        }
        if(str[i] == ‘A‘){
            num_at = (num_at+num_t) % 1000000007;///这里写个+=也错了 这就是优先级的事了
        }
        if(str[i] == ‘P‘){
            num_pat = (num_pat+num_at) % 1000000007;
        }
    }
    cout << num_pat;
    return 0;
}

**/

原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/9192900.html

时间: 2024-10-15 02:45:30

PAT Advanced level 1093的相关文章

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B 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 c

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and two flo