题目

原题链接

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. Day003 巧妙验证短路运算

    &&的短路运算 条件1&&条件2...&&条件n,程序会先判断条件1,如果条件1为false,则不判断后面的条件,直接返回false 怎么判断程序到底有 ...

  2. zTree增加树形菜单格式

    result为json字符串 //展示树形菜单 function showMenuTree(result) { console.log("页面展示函数:"+result); //属 ...

  3. Js的变量、作用域与内存

    变量.作用域与内存 1 .原始值与引用值 Undefined.Null.Boolean.Number. String和Symbol.保存原始值的变量是按值(by value)访问的 引用值是保存在内存 ...

  4. 腾讯云原生混合云-第三方集群弹EKS应对突发流量的利器

    作者 何鹏飞,腾讯云专家产品经理,曾作为容器私有云.TKEStack的产品经理兼架构师,参与腾讯云内部业务.外部客户容器化改造方案设计,目前负责云原生混合云产品方案设计工作. 胡晓亮,腾讯云专家工程师 ...

  5. [bug] vscode output 输出乱码

    参考 https://blog.csdn.net/qq_34192032/article/details/105077173 https://blog.csdn.net/a19990412/artic ...

  6. [Python] 爬虫系统与数据处理实战 Part.1 静态网页

    爬虫技术基础 HTTP/HTTPS(7层):应用层,浏览器 SSL:加密层,传输层.应用层之间 TCP/IP(4层):传输层 数据在传输过程中是加密的,浏览器显示的是解密后的数据,对爬虫没有影响 中间 ...

  7. Linux下Firefox打开文件jnlp文件

    ubuntu(linux)打开jnlp文件 咘咘 2019-05-20 15:12:48 1331 收藏展开 前提条件是安装有java环境.whereis java 查看自己java安装目录.本人是在 ...

  8. Linux占用swap分区过高,物理内存还有剩余

    Linux占用swap分区过高,物理内存还有剩余 问题分析 Swap配置对性能的影响 分配太多的Swap空间会浪费磁盘空间,而Swap空间太少,则系统会发生错误.如果系统的物理内存用光了,系统就会跑得 ...

  9. Docker Swarm(八)滚动更新、回滚服务

    滚动更新.回滚服务 默认情况下, swarm一次只更新一个副本,并且两个副本之间没有等待时间,我们可以通过: # 定义并行更新的副本数量--update-parallelism# 定义滚动更新的时间间 ...

  10. Ansible playbook编写Apache角色

    编写Apache角色:使用源码安装 在files中下载扩展包和安装包 [root@localhost project]# ls roles/httpd/files/ apr-1.6.5.tar.gz ...