题目

原题链接

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. controller通过map返回减少dto类的创建

    更多精彩关注公众号 不要把实体类对象直接返给前端 ,首先想到的是创建DTO,但是这样就造成大量的DTO,显得很臃肿,为了减少dto的数量,像一些比较少的参数避免创建不必要的DTO,通过本次优化达到业务 ...

  2. 计算机网络参考模型,IP地址及MAC地址查看方法,数据包封装过程

    分层思想 首先,计算机网络参考模型,是基于分层思想而出现的.分层思想,就是将复杂流程分解为几个功能单一的子过程. 优点: 可以让整个流程更加清晰, 让复杂问题简单化, 更容易发现问题,并真对性的解决问 ...

  3. work2_求交点数

    教学班级:周三上午三四节 项目地址:https://github.com/875571216/- PSP表格 psp2.1 Personal Software Process Stages 预估耗时( ...

  4. mysql order by 多样依照排序

    如果先按a排序升序,a相同时按b降序排序 则order by a,b desc

  5. 1. Java概述

    1.1 Java语言背景介绍(了解) 语言:人与人交流沟通的表达方式. 计算机语言:人与计算机之间进行信息交流沟通的一种特殊语言. Java语言是美国Sun公司(Stanford University ...

  6. 啥?SynchronousQueue和钟点房一个道理

    今天这篇文章,我们继续讲架构师大刘的故事. 大刘有段时间经常会给一些程序员讲课.这一方面是由于团队培训的需要,一方面也是大刘自身想搞搞凡尔赛,嘚瑟一下自身的实力. 大刘讲课是允许公司任何一个人进去听的 ...

  7. 怎么用CMD命令进入D盘

    怎么用CMD命令进入D盘 太平洋电脑网 ​ 已认证的官方帐号 6 人赞同了该文章 大家都知道win操作系统想要打开D盘,直接点我的电脑就能进d盘了,有时候只能使用dos的情况下也利用系统自带的cmd命 ...

  8. 强哥JavaScript学习笔记

    js文件放header头最后,js代码放body体最后 js语言定位: js是基于对象的语言 php.java是面向对象的语言 定义变量: var str="hello world" ...

  9. Zabbix 自定义report

    #!/bin/bash . /etc/profile logdir='/home/admin/zabbix/zabbix_log' mysql_host='localhost' mysql_user= ...

  10. Sqli-labs-master通关解析(持续更新中。。。)

    大多情况下:SQL注入其实就是构造正确的mysql命令,让网页回显本不应该让我们看到的数据(如用户的账号和密码). 第一关-联合查询注入 查库 // 查看当前页面在的数据库 ?id=-1' union ...