题目解析
有几个PAT(25)
http://www.nowcoder.com/questionTerminal/5e7d025e91ab468f909cb93d431b89c3
解析:求PAT总数,要先将问题拆分先求有效PA总数
从左遍历字符串,遇到P就累计P数量,A前面有效的P的数量就是pacount+=pcount;T同理
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String text = in.nextLine(); int pcount = 0; int pacount= 0; int totalcount = 0; for (int i=0;i<text.length();i++){ char c = text.charAt(i); if (c == 'P') { pcount++; } else if (c == 'A') { pacount+=pcount; }else { totalcount+=pacount; totalcount %= 1000000007; } } System.out.println(totalcount); } }