题目

原题链接

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

限制

  • 作者 CAO, Peng
  • 单位 Google
  • 代码长度限制 16 KB
  • 时间限制 150 ms
  • 内存限制 64 MB

题目大意

输入

由'P','A','T'三个字符组成的字符串

输出

字符串中包含"PAT"的个数。(PAT可以不连续,但要按这个顺序,只要有一个字符不一样,就可算一次。)

例子

APPAPT 包含2个"PAT"。分别是:APPAPT 和 APPAPT

题解

思路

用类似于编译原理中状态机的思路,:从左到右扫描字符串,对每个输入字符,都可以使字符串当前的状态变化。注:这不是标准意义上的状态机。

也就是设置了几个状态:start, P, PA, PAT(结束态)

  • 当前字母是P时,P状态的个数加1;
  • 当前字母是A时,所有P状态的字符串变为PA状态;
  • 当前字母是T时,所有PA状态的字符串变为PAT状态。

    扫描完字符串后,便可得出PAT状态的字符串有多少个。

代码

#include<bits/stdc++.h>
using namespace std;
#define MAX 1000000007 int main(){
string str;
cin >> str;
int p = 0, pa = 0, pat = 0;
int len = str.length();
for(int i = 0; i < len; i++){
switch(str[i]){
case 'P': p++;
p = p % MAX;
break;
case 'A': pa += p;
pa = pa % MAX;
break;
case 'T': pat += pa;
pat = pat % MAX;
break;
default: ;
}
}
cout << pat << endl;
return 0;
}

优点

  • 编码直观,简单
  • 可扩展性好,不但适用于三个字符的情况,更多字符的情况也适用。

扩展

题目

若题目变为4个字符的情况。如:输入是由'P','A','T','S'四个字符组成的字符串,输出是求字符串中包含"PATS"的个数。

思路

此时,状态机变成以下的情况:

在4个字符的情况下,设置了五个状态:start, P, PA, PAT,PATS(结束态)

  • 当前字母是P时,P状态的个数加1;
  • 当前字母是A时,所有P状态的字符串变为PA状态;
  • 当前字母是T时,所有PA状态的字符串变为PAT状态;
  • 当前字母是S时,所有PAT状态的字符串变为PATS状态。

    扫描完字符串后,便可得出PATS状态的字符串有多少个。

代码

#include<bits/stdc++.h>
using namespace std;
#define MAX 1000000007 int main(){
string str;
cin >> str;
int p = 0, pa = 0, pat = 0,pats = 0; // 增加了 pats 状态
int len = str.length();
for(int i = 0; i < len; i++){
switch(str[i]){
case 'P': p++;
p = p % MAX;
break;
case 'A': pa += p;
pa = pa % MAX;
break;
case 'T': pat += pa;
pat = pat % MAX;
break;
case 'S': pats += pat; // 增加的代码
pats = pats % MAX; // 增加的代码
break; // 增加的代码
default: ;
}
}
cout << pats << endl; // 输出 pats 的个数
return 0;
}

PAT甲级 1093 Count PAT‘s (25 分) 状态机解法的更多相关文章

  1. PAT甲级——1093 Count PAT's (逻辑类型的题目)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93389073 1093 Count PAT's (25 分)   ...

  2. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  3. PAT 甲级 1083 List Grades (25 分)

    1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...

  4. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  5. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

  6. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  7. PAT 甲级 1062 Talent and Virtue (25 分)(简单,结构体排序)

    1062 Talent and Virtue (25 分)   About 900 years ago, a Chinese philosopher Sima Guang wrote a histor ...

  8. PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  9. PAT 甲级 1056 Mice and Rice (25 分) (队列,读不懂题,读懂了一遍过)

    1056 Mice and Rice (25 分)   Mice and Rice is the name of a programming contest in which each program ...

随机推荐

  1. 使用Layui、Axios、Springboot(Java) 实现EasyExcel的导入导出(浏览器下载)

    实现EasyExcel的导入导出(浏览器下载) 实现三个按钮的功能,但是却花费了一天的时间包括总结. 使用到的技术:springboot layui axios EasyExcel mybatis-p ...

  2. mybatis新手快速搭建成功详细操作

    1.数据库建表 在数据库中新建一个名为mybatis的数据库,在mybatis数据库中新建一张 t_user 表,表中有3个字段,id,name,password,代码如下: 新建一个mybatis数 ...

  3. 使用ldap客户端创建zimbra ldap用户的格式

    cat << EOF | ldapadd -x -W -H ldap://:389 -D "uid=zimbra,cn=admins,cn=zimbra" dn: ui ...

  4. [刷题] 455 Assign Cookies

    要求 贪心算法的关键:判断问题是否可以用贪心算法解决 给小朋友们分饼干,每个小朋友"贪心指数"为g(i),饼干大小值s(i) g(i):小朋友需要的饼干大小的最小值 若s(j)&g ...

  5. mysql基础之日志管理(查询日志、慢查询日志、错误日志、二进制日志、中继日志、事务日志)

    日志文件记录了MySQL数据库的各种类型的活动,MySQL数据库中常见的日志文件有 查询日志,慢查询日志,错误日志,二进制日志,中继日志 ,事务日志. 修改配置或者想要使配置永久生效需将内容写入配置文 ...

  6. system verilog内建数据类型

  7. .NET Worker Service 添加 Serilog 日志记录

    前面我们了解了 .NET Worker Service 的入门知识[1] 和 如何优雅退出 Worker Service [2],今天我们接着介绍一下如何为 Worker Service 添加 Ser ...

  8. JS 反射机制及 Reflect 详解

    一.什么是反射机制 反射机制是在编译阶段不知道是哪个类被加载,而是在运行的时候才加载.执行. 也就是说,反射机制指的是程序在运行时能够获取自身的信息. js 中的 apply 就是反射机制. 二.Re ...

  9. 孔乙己,一名ERP顾问

    欢迎关注微信公众号:sap_gui (ERP咨询顾问之家) 公司的会议室的格局,是和别处不同的:都是中间一个大的会议圆桌,桌子上面放着各台电脑,可以随时打开ERP系统.做ERP顾问的人,傍午傍晚下了班 ...

  10. IDEA2021.1 安装教程

    工欲善其事必先利其器. 一.下载 IDEA 官方下载地址: https://www.jetbrains.com/zh-cn/idea/download/ 二.安装 IDEA 注:安装IDEA之前需要我 ...