您的位置:首页 > 其它

pat 1093. Count PAT's (25)

2015-11-29 16:30 549 查看
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

只要数每一个A前面的P和T的个数,然后相乘,再所有的A的P*T的数相加即可。#include<iostream>
using namespace std;
#include<string.h>
#include<algorithm>
#define maxn 0x7fffffff
char s[100005];
int a[100005];
int main()
{
int i,j,s1=0,s2,k,s3=0;
cin>>s;
for(j=0,i=0;s[i]!=0;i++)
if(s[i]=='A')
a[j++]=i;
int f=0,k1,k2;
s2=0;
for(k2=a[0]+1;k2<strlen(s);k2++)
if(s[k2]=='T')
s2++;
for(i=0;i<j;i++)
{
for(k1=f;k1<a[i];k1++)
if(s[k1]=='P')
s1++;
f=a[i]+1;
if(i>0)
{
for(k2=a[i-1]+1;k2<a[i];k2++)
if(s[k2]=='T')
s2--;
}
s3=(s3+s1*s2)%1000000007;
}
cout<<s3<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: