码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【PAT甲级 - C++题解】1093 Count PAT‘s


    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
    📚专栏地址:PAT题解集合
    📝原题地址:题目详情 - 1093 Count PAT’s (pintia.cn)
    🔑中文翻译:PAT 计数
    📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
    ❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

    1093 Count PAT’s

    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 105 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
    
    • 1

    Sample Output:

    2
    
    • 1
    题意

    给定一个字符串,请你求出字符串中包含的 PAT 的数量。

    思路

    这道题可以用动态规划状态机来做,将需要匹配的字符串 PAT 转换成状态,为了方便计算,我们将空格设置为初始状态 0 ,然后在从前往后遍历字符串 s 的时候就计算一遍所有的状态,每个状态都是由它前一个状态转移过来。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RxhmqWj0-1667989071807)(PAT 甲级辅导.assets/22.png)]

    状态表示: f [ i ] [ j ] f[i][j] f[i][j] 表示只考虑前 i 个字母且走到了状态 j 的所有路线的数量。

    状态计算:

    如果当前遍历的字符不属于 PAT 中的字符,则将上一轮对应状态的数量转移到这一轮中: f [ i ] [ j ] = f [ i − 1 ] [ j ] f[i][j]=f[i-1][j] f[i][j]=f[i−1][j]

    如果满足要求,则加上上一轮该状态的前一个状态的数量: f [ i ] [ j ] = ( f [ i ] [ j ] + f [ i − 1 ] [ j − 1 ] ) % M O D f[i][j]=(f[i][j]+f[i-1][j-1])\%MOD f[i][j]=(f[i][j]+f[i−1][j−1])%MOD

    代码
    #include
    using namespace std;
    
    const int N = 100010, MOD = 1000000007;
    char s[N], p[] = " PAT";
    int f[N][4];
    
    int main()
    {
        cin >> s + 1;
        int n = strlen(s + 1);
    
        f[0][0] = 1;  //初始化
        for (int i = 1; i <= n; i++)   //遍历每个字符
            for (int j = 0; j < 4; j++)    //遍历每个状态
            {
                f[i][j] = f[i - 1][j];  //将上一轮的状态转移过来
                //如果满足条件,则更新数量
                if (s[i] == p[j])  f[i][j] = (f[i][j] + f[i - 1][j - 1]) % MOD;
            }
    
        cout << f[n][3] << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    【Python】 了解二分类:机器学习中的基础任务
    SpringCloud 14 Config:客户端连接服务端
    深度学习笔记之优化算法(三)动量法的简单认识
    华为ICT——云计算基础知识、计算类技术听课笔记
    在服务器上搭建Cadence16.6 CIS共享库
    线程安全问题(模拟取钱案例)
    Yolov8-pose关键点检测:原创自研&涨点系列篇 | 一种新颖的轻量化网络,用于提升遥感图像中的小物体检测 | 2024年二区YOLOv5改进最新成果
    Go 语言为什么很少使用数组?
    LeetCode39. Combination Sum
    中国企业出海金字塔:产品出海、渠道出海和品牌出海
  • 原文地址:https://blog.csdn.net/Newin2020/article/details/127775149
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号