Pat-basic-1040-has a few pat

Source: Internet
Author: User

The string appapt contains two words "Pat", where the first Pat is 2nd (P), 4th (a), 6th (t), and the second Pat is 3rd (p), 4th bit (a), 6th (t).

Now given a string, how many pat can it form?

Input format:

Input has only one row, contains a string, the length of not more than 105, only contains P, A, T three kinds of letters.

Output format:

Outputs the number of PAT contained in a given string in a row. Because the result may be larger, only the result of the remainder is output to 1000000007.

Input Sample:

Appapt

Sample output:

2

The core idea is to count the number of p on the left side of each character a and how many t on the right.
In order to reduce the number of traversal times, we first traverse to get the number of T in the entire string
The second time from left to right traversal of the string, met P nump++, representing the next a before the number of p, met T numt--, on behalf of the next a how many T.
Finally, take a look at ll and mod on it.
#include <bits/stdc++.h>#defineLL Long Long#defineMAXN 100000+50#defineMOD 1000000007using namespacestd;CharSTR[MAXN]; LL Res=0; LL Nump=0, NUMT =0;intMain () {scanf ("%s", str); intLen =strlen (str);  for(inti =0; i < Len; ++i) {        if(Str[i] = ='T') {numt++; }    }     for(inti =0; i < Len; ++i) {        if(Str[i] = ='P') {Nump++; }        Else if(Str[i] = ='A') {res= (RES+NUMP*NUMT)%MOD; }        Else if(Str[i] = ='T') {numt--; }} cout<< Res <<Endl; return 0;}
Capouis ' CODE

Pat-basic-1040-has a few pat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.