【一隻蒟蒻的刷題歷程】 【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;
}

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