1. 程式人生 > >1040. 有幾個PAT(25)-浙大PAT乙級真題java實現

1040. 有幾個PAT(25)-浙大PAT乙級真題java實現

個人部落格:開啟連結

1040. 有幾個PAT(25)
字串APPAPT中包含了兩個單詞“PAT”,其中第一個PAT是第2位(P),第4位(A),第6位(T);
第二個PAT是第3位(P),第4位(A),第6位(T)。
現給定字串,問一共可以形成多少個PAT?
輸入格式:
輸入只有一行,包含一個字串,長度不超過105,只包含P、A、T三種字母。
輸出格式:
在一行中輸出給定字串中包含多少個PAT。由於結果可能比較大,只輸出對
1000000007取餘數的結果。
輸入樣例:
APPAPT
輸出樣例:
2


import java.io.BufferedReader;
import java.io.InputStreamReader;

public
class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); char[] s = in.readLine().toCharArray(); in.close(); int len = s.length, countp = 0, countt = 0; long total = 0; for
(int i = 0; i < len; i++) { if (s[i] == 'T') countt++; } for (int i = 0; i < len; i++) { if (s[i] == 'P') countp++; if (s[i] == 'T') countt--; if (s[i] == 'A') { total = total + countp * countt; if
(total > 1000000007) total = total % 1000000007; } } System.out.println(total); } }

這裡寫圖片描述