1040、有几个PAT

来源:互联网 发布:淘宝清洗不理他 编辑:程序博客网 时间:2024/04/27 19:44
//字符串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<span style="font-size:24px;color:#FF0000;">提示:示例一的代码不能完全通过(示例二完美通过)pat系统的测试,经过我多次查资料,是因为输入超时。我们都知道PAT考试中对程序的时间是由限制的,而用java写的程序,大多数还没有进入主程序就已经超时了,究其原因,是因为由于没有选择正确的输入语句导致。所以本博主希望大家在竞赛中慎用输入语句。</span>import java.util.Scanner;public class <span style="color:#FF0000;">示例一</span> {private static Scanner sc;private static final int mod = 1000000007;public static void main(String[] args) {sc = new Scanner(System.in);String str = sc.nextLine();char[] ch = str.toCharArray();int n = str.length();int countP = 0;int countPA = 0;int countPAT = 0;for (int i = 0; i < n; i++) {if (ch[i] == 'P') {countP++;} else if (ch[i] == 'A') {countPA = (countP + countPA) % mod;} else if (ch[i] == 'T') {countPAT = (countPA + countPAT) % mod;}}System.out.println(countPAT);}}<pre name="code" class="java">import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class<span style="color:#FF0000;"> 示例二</span> {private static final int mod = 1000000007;public static String reader(InputStream in) {BufferedReader reader = new BufferedReader(new InputStreamReader(in));try {return reader.readLine();} catch (IOException e) {e.printStackTrace();}return null;}public static void main(String[] args) {String str=reader(System.in);char[] ch = str.toCharArray();int n = str.length();int countP = 0;int countPA = 0;int countPAT = 0;for (int i = 0; i < n; i++) {if (ch[i] == 'P') {countP++;} else if (ch[i] == 'A') {countPA = (countP + countPA) % mod;} else if (ch[i] == 'T') {countPAT = (countPA + countPAT) % mod;}}System.out.println(countPAT);}}

 

0 0