1093. Count PAT's (25)
2016-07-28 17:41
357 查看
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.
Input Specification:
Each input file contains one test case. For each case, there is only one line giving a string of no more than 105 characters containing only P, A, or T.
Output Specification:
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.
Sample Input:
Sample Output:
找出一串字符串中有多少个“PAT”。思路:要找PAT ,首先要找到PA ,然后继续往上推就是要先找P 。遍历字符串,如果遇到T ,累加前面的PA的数量来更新PAT 的数量,如果遇到A,累加前面的P 的数量来更新PA 的数量,如果遇到P ,更新P 的数量。最后得到的PAT 的数量就是答案。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
string s;
cin>>s;
long long num_p=0,num_pa=0,num_pat=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='T')
{
num_pat+=num_pa;
}
else if(s[i]=='A')
{
num_pa+=num_p;
}
if(s[i]=='P')
{
num_p++;
}
}
cout<<num_pat%1000000007<<endl;
}
Now given any string, you are supposed to tell the number of PAT's contained in the string.
Input Specification:
Each input file contains one test case. For each case, there is only one line giving a string of no more than 105 characters containing only P, A, or T.
Output Specification:
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.
Sample Input:
APPAPT
Sample Output:
2
找出一串字符串中有多少个“PAT”。思路:要找PAT ,首先要找到PA ,然后继续往上推就是要先找P 。遍历字符串,如果遇到T ,累加前面的PA的数量来更新PAT 的数量,如果遇到A,累加前面的P 的数量来更新PA 的数量,如果遇到P ,更新P 的数量。最后得到的PAT 的数量就是答案。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
string s;
cin>>s;
long long num_p=0,num_pa=0,num_pat=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='T')
{
num_pat+=num_pa;
}
else if(s[i]=='A')
{
num_pa+=num_p;
}
if(s[i]=='P')
{
num_p++;
}
}
cout<<num_pat%1000000007<<endl;
}
相关文章推荐
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- javascript数组操作总结和属性、方法介绍
- mysql_fetch_assoc和mysql_fetch_row的功能加起来就是mysql_fetch_array
- JavaScript Array扩展实现代码
- JavaScript:Array类型全面解析
- JavaScript之数组(Array)详解
- Javascript实现Array和String互转换的方法
- C#中Array与ArrayList用法及转换的方法
- Array栈方法和队列方法的特点说明
- Array.prototype.slice 使用扩展
- Array, Array Constructor, for in loop, typeof, instanceOf
- 实例详解ECMAScript5中新增的Array方法
- Javascript数组Array基础介绍
- js Array的用法总结
- JS Array创建及concat()split()slice()的使用方法
- JavaScript 判断判断某个对象是Object还是一个Array
- JavaScript 数组- Array的方法总结(推荐)
- Javascript中的Array数组对象详谈
- js模拟实现Array的sort方法
- 在javascript将NodeList作为Array数组处理的方法