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 105characters 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
1 #include<string> 2 #include<iostream> 3 #include<map> 4 5 using namespace std; 6 7 int numofP[100001]; 8 int numofPA[100001]; 9 int main() 10 { 11 string str; 12 getline(cin,str); 13 int P,A,T; 14 T = 0; 15 int sumofP = 0; 16 for(int i = 0 ; i < str.length() ;++i) 17 { 18 if(str[i] == ‘P‘) 19 ++sumofP; 20 numofP[i] = sumofP; 21 } 22 23 int sumofPA = 0; 24 for(int i = 0 ; i < str.length() ;++i) 25 { 26 if(str[i] == ‘A‘) 27 { 28 sumofPA += numofP[i]; 29 } 30 numofPA[i] = sumofPA; 31 } 32 33 int sumofPAT = 0; 34 for(int i = 0 ; i < str.length() ;++i) 35 { 36 if(str[i] == ‘T‘) 37 { 38 sumofPAT += numofPA[i]; 39 sumofPAT = sumofPAT % 1000000007;//坑点 40 } 41 } 42 cout << sumofPAT <<endl; 43 return 0; 44 }
1093. Count PAT's (25)
时间: 2024-10-13 10:07:22