By 1003 I want to! (20 points)
"Correct answer" is the most joy automatic reply sentence topic given system. This title belongs to the PAT of "correct answer" big delivery - just read the string following conditions are satisfied, the system will output "correct answer", otherwise outputs "Wrong Answer."
Get the "correct answer" conditions are:
String must have only P, A, T three character, other characters can not contain;
arbitrary shape such as a string can be obtained xPATx "correct answer", or where x is the empty string, or only by the letter a string thereof;
if aPbTc is correct, then aPbATca is correct, wherein a, b, c or all the empty string, or a string consisting only of letters a.
Now ask you to write a PAT referee program automatically determines which strings can get "the answer right".
Input format:
Each test comprises a test input. Line 1 is given a positive integer n (<10), is the number of strings to be detected. Next, one row for each string, the string length of not more than 100, no spaces.
Output formats:
the detection result row for each string, if the string can get "correct answer", the output YES, otherwise output NO.
Yang_li shu_ru:
8
PAT at
PAAT
AAPATAA
AAPAATAAAA
xPATx
the PT
Whatever the
APAAATAA
Output Sample:
YES
YES
YES
YES
NO
NO
NO
NO
analysis
Topic analysis
Two conditions are required to answer correctly
- PAT only three character classes
- xPATx, aPbTc, aPbATca of x, a, b, c may be empty or may be a string of A or A.
- Sample heard a summary to get in front of P between a * P and T = a subsequent T (summary of the law, I did not find the reference of willow God's blog)
Learn to master
#include <iostream>
#include <map>
using namespace std;
int main(){
int n,p = 0,q = 0;
cin >> n;
while (n--!=0) {
string s;
cin >> s;
map<char,int> m;
for (int i=0; i<s.size();i++) {
m[s[i]] ++;
if(s[i]=='P') p = i;
if(s[i]=='T') q = i;
}
if(m['P']==1&&m['T']==1&&m['A']!=0&&m.size()==3&&q-p!=1&&((s.size()-q-1)==(q-p-1)*(p)))
printf("YES\n");
else printf("NO\n");
}
return 0;
}
java version
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
sc.nextLine();
while(n--!=-1){
String str = sc.nextLine();
pat_test(str);
}sc.close();
}
static void pat_test(String str){
String s = str;
if(s.contains("P")&&s.contains("A")&&s.contains("T")){
s=s.replace("P","");
s=s.replace("A","");
s=s.replace("T","");
s=s.replace("\\s","");
if(s.isEmpty()){
int a =str.indexOf("P");
int c =str.indexOf("T");
int b =c-a-1;
int l=str.length()-c-1;
if(a*b==l){
System.out.println("YES");
}else System.out.println("NO");
}else System.out.println("NO");
}else System.out.println("NO");
}
}