admin 管理员组

文章数量: 1184232

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.

Input Specification:

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.

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数量,和A之后的T的数量。用两个循环用来找P和T。
我在此处出现了VS上可以编译,但是PTA上编译错误的问题,原因在于头文件写错。strlen(ch),此处是char ch[]。应该加#include < cstring>。但我加成了C++的string头文件。

#include<cstdio>#include<string.h>#define LL long long usingnamespace std;intmain(){char ch[100009];int ans[100009];scanf("%s",ch);int n =strlen(ch);int x =0;int a =0;
	LL ansN =0;int t =0;for(int i =0; i < n; i++){if(ch[i]=='P')
			x++;elseif(ch[i]=='A'){
			ans[a++]= x;}}for(int i = n-1; i >=0; i--){if(ch[i]=='T')
			t++;elseif(ch[i]=='A'){
			ansN +=(ans[--a]* t);}}printf("%lld", ansN%1000000007);return0;}

本文标签: 从基础到 攻克 高分