1093. Count PAT's (25)
来源:互联网 发布:医疗大数据公司排名 编辑:程序博客网 时间:2024/04/30 10:14
1.这道题目比较有趣,我的思路如下:
1)首先统计countP[i],即0~i的位置上,一共有多少个P,同理可以反向统计出countT[i],即i到n-1之间有多少个T
2)遍历string,当str[i]==‘A’时,那么ans+=countP[i-1]*countT[i+1],当然, 需要根据题目要求,对100000007取模
AC代码如下:
//#include<string>//#include <iomanip>#include<vector>#include <algorithm>//#include<stack>#include<set>#include<queue>#include<map>//#include<unordered_set>#include<unordered_map>//#include <sstream>//#include "func.h"//#include <list>#include<stdio.h>#include<iostream>#include<string>#include<memory.h>#include<limits.h>using namespace std;#define maxDivide 1000000007int main(void){string str;cin >> str;int *countP = new int[str.size()];int *countT = new int[str.size()];memset(countP, 0, sizeof(countP));memset(countT, 0, sizeof(countT));if (str[0] == 'P') countP[0] = 1;if (str[str.size() - 1] == 'T') countT[str.size() - 1] = 1;for (int i = 1; i < str.size(); i++){if (str[i] == 'P')countP[i] = countP[i - 1] + 1;elsecountP[i] = countP[i - 1];}for (int i = str.size()-2; i >=0; i--){if (str[i] == 'T')countT[i] = countT[i + 1] + 1;elsecountT[i] = countT[i + 1];}unsigned long long ans = 0;for (int i = 1; i < str.size() - 1; i++){if (str[i] == 'A'){unsigned long long tmp = (countP[i - 1] * countT[i + 1]) % maxDivide;ans += tmp;ans = ans%maxDivide;}}cout << ans << endl;return 0;}
0 0
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- 1093. Count PAT's (25)
- poj 2516 Minimum Cost(最小费用最大流 spfa算法求最短路)
- gcc 编译器中 printf i++ 和 ++i 的输出
- Romberg(龙贝格)求积公式求解数值积分时的注意事项
- 单链表的实现
- Linux Advance--线程和fork
- 1093. Count PAT's (25)
- 多线程 NSOperation
- Google Protocol Buffers介绍
- samba ubuntu下快速配置
- C++ 学习(虚基类)
- FindBugs插件的安装与使用
- SDWebImage 第三方框架
- 安卓学习知识点总结一
- 源码解析Volley框架