【一只蒟蒻的刷题历程】 【PAT】 A1093 计数PAT

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


题目大意:

字符串APPAPT包含两个PAT作为子字符串。 第一个由第二,第四和第六个字符组成,第二个由第三,第四和第六个字符组成。

现在给定任何字符串,您应该知道字符串中包含的PAT的数量。

输入规格:

每个输入文件包含一个测试用例。 对于每种情况,只有一行给出不超过10 5个字符的字符串,其中仅包含P,A或T.

输出规格:

对于每个测试用例,在一行中打印字符串中包含的PAT的数量。 由于结果可能很多,因此只需输出以1000000007设置的结果。

样本输入:

APPAPT

样本输出:

2


思路:

构成PAT,每个A 前面的 ‘P’个数 乘 后面的‘T’ 个数就是总的个数。

代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;  
const int maxn=1e5+50;
const int mod=1000000007;  //取余
int main() 
{
  int pre[maxn];
  long long ans=0; //答案
  string s;
  getline(cin,s); 
  int nump=0;  //每个A前面的p个数
  for(int i=0;i<s.length();i++)
  {
  	 if(s[i] == 'P') nump++;
  	 else if(s[i] == 'A') pre[i]=nump;
  }
  
  int numt=0; //每个A后面的t个数
   for(int i=s.length()-1;i>=0;i--)
   {
   	  if(s[i]=='T') numt++;
   	  else if(s[i]=='A') ans = (ans+numt*pre[i])%mod; //取余
   }
   cout<<ans;
  return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章