您的位置:首页 > 其它

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:
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