【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;
}
相关推荐
_李小白26 分钟前
【C++学习笔记】新特性之inline变量
c++·笔记·学习
心中有国也有家29 分钟前
hccl 架构拆解:昇腾集合通信库到底在做什么?
人工智能·经验分享·笔记·分布式·算法·架构
桀人1 小时前
C++——模板初阶(收录在专栏C++入门到精通)
开发语言·c++
小O的算法实验室1 小时前
2026年MCS,Q-learning增强MOPSO与改进DWA融合算法+复杂三维地形下特定移动机器人动态路径规划
算法
Lumbrologist1 小时前
【C++】零基础入门 · 第 2 节:变量、基本数据类型与输入输出
java·开发语言·c++
XX風1 小时前
CMake / Make / Ninja / MSVC / GCC / Clang / MSBuild —— 完整体系化理解
c++
Peter·Pan爱编程2 小时前
10. new_delete 不是 malloc_free 的包装
c++·人工智能·算法
故事和你913 小时前
洛谷-【动态规划1】动态规划的引入2
开发语言·数据结构·c++·算法·动态规划·图论
重生之我是Java开发战士3 小时前
【动态规划】背包问题:完全背包,二位费用的背包问题,似包非包
算法·动态规划
LabVIEW开发4 小时前
LabVIEW实现FDTD 电磁仿真
算法·labview·labview知识·labview功能·labview程序