|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目如下
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
现在就请你为PAT写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。
输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。
输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。
输入样例:
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
输出样例:
YES
YES
YES
YES
NO
NO
NO
NO
帖子答案代码(不一定正确)
- #include<iostream>
- #include<string.h>
- using namespace std;
- int main(){
- int n,cout_p,cout_a,cout_t,pos_p,pos_t;
- char a[101];
- int b[100];
- cin>>n; # cin 是什么东西,这个语句什么意思
- for(int i=0;i<n;++i){
- cin>>a;
- cout_p=cout_a=cout_t=pos_p=pos_t=0;
- int c=strlen(a);
- for(int j=0;j<c;++j)
- {
- if(a[j]=='A')
- cout_a++;
- if(a[j]=='P'){
- cout_p++;
- pos_p=j;
- }
- if(a[j]=='T'){
- cout_t++;
- pos_t=j;
- }
- }
-
- if(cout_a+cout_p+cout_t!=strlen(a) || cout_p>1 ||cout_t>1 || pos_p+1>=pos_t || pos_p*(pos_t-pos_p-1)!=strlen(a)-pos_t-1) // 这里是用来干什么的
- b[i]=0;
- else b[i]=1;
- }
- for(int i=0;i<n;++i){
- if(b[i]==0) cout<<"NO"<<endl;
- else cout<<"YES"<<endl;
- }
- } 
复制代码
试下代码,看看满分没。代码应该很容易看懂:
- #include <cstdio>
- #include <cstring>
- int main()
- {
- int t;
- scanf("%d\n",&t);
- char c[1005];
- while(t --)
- {
- int l=0;
- int flag=0;
- int locat;
- scanf("%s",c);
- int a;
- for( a = 0; a < strlen(c); a ++)
- {
- if(flag&&c[a]!='A')
- {
- printf("%d\n",a);
- flag=0;
- }
- if(c[a]!='P'&&c[a]!='A'&&c[a]!='T')
- break;
- if(c[a]=='P'&&c[a+1]=='A'&&c[a+2]=='T')
- {
- locat=a;
- a+=2;
- flag=1;
- }
- if(c[a]=='P')
- {
- l=0;
- a++;
- while(c[a]=='A')
- {
- l++;
- a++;
- }
- if(l==0)
- {
- continue;
- }
- if(c[a]=='T')
- {
- flag=3;
- locat=a-l-1;
- a++;
- }
- }
- }
- if(flag)
- {
- for( a = 0; a < locat; a ++)
- {
- if(c[a]!='A')
- {
- flag=0;
- break;
- }
- }
- }
- if(flag==1&&locat>strlen(c)-3-locat)
- flag=0;
- if(flag==3&&locat*l!=strlen(c)-locat-l-2)
- {
- //printf("%d %d\n",locat*l,strlen(c)-locat-l-2);
- flag=0;
- }
- if(flag)
- printf("YES\n");
- else
- printf("NO\n");
- }
- return 0;
- }
复制代码
|
|