题目描述
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. ubuntu 文件管理器 异常 强制关闭

    搜索进程号 ps -A | grep nautilus 杀掉 sudo kill xxxxx

  2. C/C++程序基础-C++与C有什么不同

    1:C和C++的联系和区别? 答:C是一个结构化语言,它的重点在于算法和数据结构.对于语言本身而言,C是C++的子集.C程序的设计首先要考虑的是如何通过一个过程,对输入进行运算处理,得到输出.对于C+ ...

  3. Linux服务器操作

    Linux关于服务器的操作(root权限) 1. 查看服务器版本信息 cat /etc/redhat-release 2.将Windows上文件通过xshell传到Linux服务器 查找系统自带软件包 ...

  4. RHEL 7.6系统安装配置图解教程

  5. 在业务控制方法中写入User,Admin多个模型收集参数

    1)  可以在业务控制方法中书写1个模型来收集客户端的参数 2)  模型中的属性名必须和客户端参数名一一对应 3)  这里说的模型不是Model对象,Model是向视图中封装的数据 @Controll ...

  6. tomcat简单快捷改端口

  7. eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录

    老求了,好多东西记不住了,再不记以后怕是记不住了. eclipse JAVAEE for web Version: Mars.2 Release (4.5.2) tomcat 7.0.29 sprin ...

  8. 1. hadoop使用启动命令时报错之分析解决

    今天在学习hadoop启动命令的时候,先jps看了下,发现namenode.datanode都开着,所以想要先停止这些服务,结果输入命令后报错:“WARN util.NativeCodeLoader: ...

  9. A股委托类型

    上海交易所于2006年8月7日启用了两种类型的市价委托,分别为:“最优五档即时成交剩余撤销”.“最优五档即时成交剩余转限价”. 深圳证券交易所启用了“对手方最优价格委托”.“本方最优价格委托”.“最优 ...

  10. UEFI+GPT下安装Win10和Ubuntu16.04双系统相关问题(引导、无线连不上网)

    1.安装双系统 1)先制作U盘启动,制作过程不再赘述 2)进入bios,设置从U盘启动 3)如下图,选择第二项为安装Ubuntu 4)前几不没什么问题,就直接往下走,选择语言,往下拉有中文选项 5)下 ...