题目描述
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. 微信内置浏览器不支持APK附件下载的原因

    由于微信的限制,应用文件在内置浏览器中下载全部被屏蔽掉,造成很多人用微信扫描二维码下载时点击下载按钮没反应,我想到的是做一个提示用户在浏览器中打开下载.其实原来很简单,就是判断当前是在微信内置浏览器中 ...

  2. python 系统模块 OS

    os.system("系统命令")  调用系统命令 os.system("task kill /f /im 系统的进程") 关闭系统进程 os.listdir( ...

  3. python_re模块

    正则表达式:http://www.regexlab.com/zh/regref.htm

  4. CF892D—Gluttony(思维,好题)

    http://codeforces.com/contest/892/problem/D D. Gluttony You are given an array a with n distinct int ...

  5. Java第00周作业

    ---恢复内容开始--- 1.你对专业的认识和期望是什么? 徐老师说过,C语言是很多语言的基础,学好C语言,将来接触的不论是面向对象还是面向过程的语言都能沉下心,能轻松解决面临的问题,处理棘手的麻烦, ...

  6. Qt Model/View 的简单说明

    目录: (一) Qt Model/View 的简单说明 .预定义模型 (二)使用预定义模型 QstringListModel例子 (三)使用预定义模型QDirModel的例子 (四)Qt实现自定义模型 ...

  7. 在 bat 批处理中运行多次 mvn

    在 bat 中运行 mvn 命令会出现这种情况,构建命令执行完成后会停留在的 mvn.bat 中,必需手工输入 exit 后,才会回到原来的脚本中继续运行.这是怎么回事? 到 maven 的安装目录下 ...

  8. Spring Security登录超时,angular ajax请求出错自动跳转至登录页(jQuery也适用)

    公司开发采用Spring Security+AngualerJS框架,在session过期之后,ajax请求会直接出错.本文介绍如何实现出错情况下自动跳转至登录页. 整体思路是,session过期后, ...

  9. Elasticsearch安装配置问题

    1.配置服务 ip 和 端口 进入 elasticsearch 安装目录,打开 config/elasticsearch.yml 配置 net.host 和 http.port net.host: 0 ...

  10. Kettle实现从mysql中取2张表数据关联的数据,并写入到mongodb中

    1 建立转换,并设置DB连接到mysql 选中DB连接:连接类型选择MySQL,输入主机名称,数据库名称,端口号,用户名,密码 输入连接名称,点击确定.(可以先点击测试,测试一下是否连接成功) 如下图 ...