【PAT】1093. Count PAT's (25)【模拟题】

题目描述

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.

翻译:字符串“APPAPT”中包含了两个子串“PAT”。第一个子串是由第二个字符、第四个字符、第六个字符构成,第二个子串是由第三个字符、第四个字符、第六个字符构成。
现在给你任意一个字符串,你需要说出其中包含了多少个“PAT”子串。

INPUT FORMAT

Each input file contains one test case. For each case, there is only one line giving a string of no more than 10^​5。characters containing only P, A, or T.

翻译:每个输入文件包括一组测试数据。对于每组测试数据,只有一行给定的不超过10^5个字符的字符串。仅仅包含‘P’,‘A’,‘T’字符。

OUTPUT FORMAT

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.

翻译:对于每组测试数据,输出一行该字符串包含的”PAT“子串的数量。由于结果可能很大,所以你只需输出模除1000000007之后的结果。


Sample Input:

APPAPT


Sample Output:

2


解题思路:

模拟题,用变量P记录到当前位置为止共出现了多少个‘P’,用变量tmpCount记录到当前位置共出现了多少次PA组合,状态转移式为tmpCount[i+1]=tmpCount[i]+P,由于我们只需要保存最后的tmpCount,所以一个变量即可。注意模除,具体实现见代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
#define bug puts("Hello\n")
#define M 1000000007
using namespace std;
char s[100010];
int ans=0;
int tmpCount=0; 
int P=0;
int main(){
	scanf("%s",s);
	int length=strlen(s);
	for(int i=0;i<length;i++){
		if(s[i]=='P')P++;
		if(s[i]=='A')tmpCount=tmpCount%M+P;
		if(s[i]=='T')ans=ans%M+tmpCount%M;		
	}
	printf("%d\n",ans%M);
	return 0;
}




版权声明:本文为tjj1998原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。