PATB1040/A1093 有几个PAT
题目描述
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的更多相关文章
- A1093 Count PAT's (25 分)
一.技术总结 这是一个逻辑题,题目大职意思是可以组成多少个PAT,可以以A为中心计算两边的P和T,然后数量乘积最后相加便是答案. 还有一个注意的是每次相加后记得mod,取余,不要等到最后加完再取余,会 ...
- 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 ...
- PAT题目AC汇总(待补全)
题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...
- PAT_A1093#Count PAT's
Source: PAT A1093 Count PAT's (25 分) Description: The string APPAPT contains two PAT's as substrings ...
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
- PAT Judge
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
- PAT 1041. 考试座位号(15)
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...
- PAT 1040. 有几个PAT(25)
字符串APPAPT中包含了两个单词"PAT",其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问 ...
随机推荐
- neo4j 一些常用的CQL
创建节点.关系 创建节点(小明):create (n:people{name:’小明’,age:’18’,sex:’男’}) return n; 创建节点(小红): create (n:people{ ...
- Mac Appium环境搭建
安装brew ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" 安装java brew install ...
- CF981D
CF981D 题意: 给你n个数,要求你分成k堆.每堆的内部加和,每堆之间是相与.问最大的值. 解法: 二进制下最大的数的所有位一定是1,所以贪心去找是否最大一定是正确的. 然后DP记录+贪心就可以A ...
- 必懂知识——HashMap的实现原理
HashMap的底层数据结构 1.7之前是:数组+链表 数组的元素是Map.Entiry对象 当出现哈希碰撞的时候,使用链表解决, 先计算出key对应的数组的下标,这个数组的这个位置上为空,直接放入, ...
- LeetCode 简化路径(探索字节跳动)
题目描述 给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path ...
- LeetCode 128. 最长连续序列(Longest Consecutive Sequence)
题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...
- 相似系数_杰卡德距离(Jaccard Distance)
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&ut ...
- 使用pyinstaller 打包python程序
1.打开PyCharm的Terminal,使用命令pip install pyinstaller安装pyinstaller 2.打包命令:pyinstaller --console --onefile ...
- CSS 优先级法则
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...
- Oracle常用CURD
-------------------------------------------------------------------------------------通用函数和条件判断函数 使用N ...