PAT B exam - there are several PAT

APPAPT string contains two words in the PAT, wherein the first PAT are two §, bit 4 (A), bit 6 (T); second PAT is bit 3 §, bit 4 ( A), bit 6 (T).
Now given string, ask how many total PAT can be formed?

Input formats:

Input single line comprising a string of not more than 10
. 5
, comprising only P, A, T three kinds of letters.

Output formats:

PAT output to a given number string on one line. As a result may be relatively large, only the output of 1,000,000,007 take the results of the remainder.

Sample input:

APPAPT

Sample output:

2

Ideas (algorithms notes)

Here Insert Picture Description
Here Insert Picture Description

#include <cstdio>
#include <cstring>
const int maxsize=100010;
const int MOD=1000000007;
char s[maxsize];
int leftP[maxsize]={0};

int main()
{
	scanf("%s",&s);
	int i,slen=strlen(s);
	if(s[0]=='P')
		leftP[0]++;
	for(i=1; i<slen; i++)
	{
		if(s[i]=='P')
			leftP[i]=leftP[i-1]+1;
		else
			leftP[i]=leftP[i-1];
	}
	int sum=0,rightT=0;
	for(i=slen-1; i>=0; i--)
	{
		if(s[i]=='T')
			rightT++;
		else if(s[i]=='A')
			sum=(sum+leftP[i]*rightT)%MOD;	//先加再MOD
	}
	printf("%d",sum);
 	return 0;
}

Published 35 original articles · won praise 2 · Views 897

Guess you like

Origin blog.csdn.net/qq_45735810/article/details/104131047
PAT