题目描述
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
​​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

全部AC

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 101000;
const int MOD = 1000000007; int main() {
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
char str[maxn];
scanf("%s", str);
int len = strlen(str);
// for(int i = 0; i < len; i++) {
// printf("%c", str[i]);
// }
int times[3] = {0};
long num = 0; //可以形成PAT的个数
int leftNumP[maxn] = {0}, rightNumT[maxn] = {0};
int temp = 0;
//遍历记录每一位左边字母P的数量
for(int i = 0; i < len; i++) {
if(str[i] == 'P'&& temp == 0) {
++leftNumP[i];
temp += 1;
} else if(str[i] == 'P' && temp != 0) {
leftNumP[i] = temp;
++leftNumP[i];
temp += 1;
} else leftNumP[i] = temp;
}
temp = 0;
long long ans = 0;//答案
//遍历记录每一位右边字母为T的数量
for(int i = len; i >= 0; i--) {
if(str[i] == 'T' && temp == 0) {
++rightNumT[i];
temp += rightNumT[i];
} else if(str[i] == 'T' && temp != 0) {
rightNumT[i] = temp;
++rightNumT[i];
temp += 1;
} else rightNumT[i] = temp;
}
for(int i = 0; i < len; i++) {
if(str[i] == 'A') {
ans = (ans + leftNumP[i] * rightNumT[i]) % MOD;
}
}
// for(int i = 0; i < len; i++) {
// printf("leftNumP[%d]:%d rightNumT[%d]:%d\n", i, leftNumP[i], i, rightNumT[i]);
// }
//直接暴力会超时
// for(int i = 0; i < len; i++){
// if(i != 0 && str[i] == 'A') {
// int leftNumP = 0; //左边字母P的数量
// int rightNumT = 0;//右边字母T的数量
// for(int j = 0; j < i; j++) {
// if(str[j] == 'P') leftNumP++;
// }
// for(int j = i; j < len; j++) {
// if(str[j] == 'T') rightNumT++;
// }
// num += leftNumP * rightNumT;
// //printf("i=%d leftNumP:%d rightNumT:%d num:%ld\n", i, leftNumP, rightNumT, num);
// }
// }
// int ans = num % 1000000007;
printf("%lld", ans);
return 0;
}

PATB1040/A1093 有几个PAT的更多相关文章

  1. A1093 Count PAT's (25 分)

    一.技术总结 这是一个逻辑题,题目大职意思是可以组成多少个PAT,可以以A为中心计算两边的P和T,然后数量乘积最后相加便是答案. 还有一个注意的是每次相加后记得mod,取余,不要等到最后加完再取余,会 ...

  2. PAT甲级——A1093 Count PAT's【25】

    The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...

  3. PAT题目AC汇总(待补全)

    题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...

  4. PAT_A1093#Count PAT's

    Source: PAT A1093 Count PAT's (25 分) Description: The string APPAPT contains two PAT's as substrings ...

  5. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  6. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

  7. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

  8. PAT 1041. 考试座位号(15)

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

  9. PAT 1040. 有几个PAT(25)

    字符串APPAPT中包含了两个单词"PAT",其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问 ...

随机推荐

  1. eclipse中不能找到dubbo.xsd

    使用dubbo时遇到问题: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'htt ...

  2. FTP基础

      FTP服务 只要有一种数据存储格式 :解析库 ,不同解析库需要不同查询方式 nsswitch 框架 平台 (每一种程序自己不再负责实时名称解析的功能,而是将这种功能委托给nsswitch) S/M ...

  3. iptables 有关计算机名解析问题

    遇到一奇怪现象: 1.开启防火墙(iptables)所有的用计算机名解析的连接全部是失效,但是用IP的一切正常.ping计算机名不通.但是ping IP地址一切OK: 2.关闭防火墙(iptables ...

  4. easyExcel简介#

    摘自:https://www.cnblogs.com/54chensongxia/p/11990312.html easyExcel简介# Java领域解析.生成Excel比较有名的框架有Apache ...

  5. js动态往对象里边添加一项

    第一种方法let obj ={"name":"tom","age":16}let key = "id";let valu ...

  6. How to install WireShark on Linux

    https://linuxtechlab.com/install-wireshark-linux-centosubuntu/

  7. FreeMarker学习(内建函数参考)

    内容参考:http://freemarker.foofun.cn/dgui_quickstart_basics.html 一.字符串内建函数 boolean: 字符串转为布尔值.字符串必须是 true ...

  8. 后盾网lavarel视频项目---Vue项目使用vue-awesome-swiper轮播插件

    后盾网lavarel视频项目---Vue项目使用vue-awesome-swiper轮播插件 一.总结 一句话总结: vue中的插件的使用和js插件的使用一样的简单,只是vue插件的引入过程有些不同 ...

  9. antd源码分析之——对话框(modal)

    目录 一.组件结构 1.antd代码结构 2.rc-ant代码结构 3.组件结构 二.antd组件调用关系及功能详解 1.Model.tsx 2.confirm 三.rc-dialog详解 1.e.t ...

  10. Oracle 报错 ORA-03290的处置

    MySql 的tuancate命令是直接truncate tableName,但在Oracle需要写成truncate table tableName,改正就好了. --END-- 2019.10.1 ...