【PAT甲级真题】- Count PAT‘s (25)

题目来源

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.

输入描述:

Each input file contains one test case. For each case, there is only one line giving a string of no more than 10 5 10^5 105

characters containing only P, A, or T.

输出描述:

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.

输入例子:

复制代码
APPAPT

输出例子:

复制代码
2

思路简介

一道经典的 DP 题

推理一遍就是:

  1. 如果当前 "PA" 的数目是 s1,"PAT" 的数目是 s2,那么输入一个 "T" ,它可以和前面所有的 "PA" 组成新的 "PAT" ,总的 "PAT" 就是新的加上旧的,即 s1+s2
  2. 同理如果当前 "P" 的数目是 s0,"PA" 的数目是 s1,那么输入一个 "A" ,它可以和前面所有的 "P" 组成新的 "PA" ,新的加上旧的即 "PA" 的总数

转移方程就是

复制代码
if cin==T:
	PAT=PAT+PA
else if cin== A:
	PA=PA+P
else:
	P++

遇到的问题

无,一遍过

代码

cpp 复制代码
/**
 * https://www.nowcoder.com/pat/5/problem/4039
 * dp
 */
#include<bits/stdc++.h>
using namespace std;


void solve(){
    string s;
    cin>>s;
    int len=s.size();
    int p=0,pa=0,pat=0;
    for(int i=0;i<len;++i){
        if(s[i]=='P')p++;
        if(s[i]=='A')pa=pa+p;
        if(s[i]=='T')pat=(pa+pat)%1000000007;
    }
    cout<<pat;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //fstream in("in.txt",ios::in);cin.rdbuf(in.rdbuf());
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}
相关推荐
手写码匠8 分钟前
Android 17 灵魂拷问深度解析:隐私、大屏、AI 端侧全面适配实战
人工智能·深度学习·算法·aigc
ysa05103036 分钟前
优先队列贪心dp
c++·笔记·算法·板子
草莓熊Lotso1 小时前
【Linux网络】深入理解Linux IO多路复用:select服务器完善、内核原理与poll实战
linux·运维·服务器·c语言·网络·c++
Andy1 小时前
Cpp进阶语法详解
c++
道影子1 小时前
《道德经》031兵者不祥,胜以丧礼处之
人工智能·深度学习·算法
花生了什么事o1 小时前
分布式 ID 生成方案:从数据库自增到雪花算法
数据库·分布式·算法
king_linlin1 小时前
算法基础——算法复杂度
c语言·开发语言·数据结构·算法
此生决int2 小时前
深入理解C++系列(04)——类和对象(下)
开发语言·c++
king_linlin2 小时前
数据结构——顺序表(附图文讲解|超详细)
c语言·数据结构·算法
GAOJ_K2 小时前
老旧产线弧形导轨换新:同规格替换避坑选型思路
人工智能·算法·机器学习·制造·导轨偏磨