PAT1003. 我要通过!(C/C++语言实现,多种解法)

1003. 我要通过!(20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于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 <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
	char a[101];
}SB;
int main()
{
	SB *b;
	char c[100];
	int m,i,n,j,k,x,y,t,q,r,s;
	b=(SB *)malloc(sizeof(SB)*101);
	scanf("%d",&m);
	for(i=0;i<m;i++)
		scanf("%s",b[i].a);
	for(i=0;i<m;i++)
	{
		n=0,k=0;
		for(j=0;j<100;j++)
		{
			c[j]=b[i].a[j];
			if(c[j]=='\0')break;
			k++;
		}
		s=0,q=0,r=0;
	//	for(j=0;j<100;j++)修改100为k,到100浪费空间 
		for(j=0;j<k;j++)
		{
			if(b[i].a[j]=='P'||b[i].a[j]=='A'||b[i].a[j]=='T')
			{
				if(b[i].a[j]=='P')
					r=j;
				if(b[i].a[j]=='T')
					q=j;
				if(q>0)
					//s=q-r;修改,因为s>=1了
					s=q-r-1; 
				n++;
			}
		}
		//if(n==k&&s<=3&&s>=2)//不好,修改为
		 if((r*s==n-q-1)&&s>=1)//P前面的A字符个数x,P和T之间的A字符个数y,T后面A字符个数z,三者满足x*y=z。
			printf("YES\n");
		else
			printf("NO\n");
	}
	
	free(b);//用malloc结束后,最好释放下空间 
	return 0;
}*/

点评:以上程序用了(结构体---指针---malloc()函数---break知识

 ---数组等等),感觉花哨了(虽然修改后,测试全通过)。

/*题解·:
1.只能有一个P和T且,P必须在T前面;
2.其他字符,要么是A要么是空格;
3.P前面的A字符个数x,P和T之间的A字符个数y,T后面A字符个数z,三者满足x*y=z。 
注意:
这三个条件是有嵌套关系的,2、3两个条件是建立在条件1成立的基础之上的,
条件3是建立在条件1和2成立的基础之上的。
因为条件3和条件2是必须同时满足才能算正确,所以样例中输入PT,才会输出“NO”。
实现方法:
1.首先排除掉不符合条件的数据,即出现了PAT以外的字符和P,T出现过两次及以上的数据;
2.再找出P,A,T的下标,根据下标来判断是否满足条件2和条件3,
然后根据是否满足条件,输出YES or NO即可。
*//*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;

int main(){
    int n;
    char c[101];
  
    scanf("%d", &n);
    for(int i=0;i<n;i++){
        bool result = true;
        int count1 = 0, count2 = 0, count3 = 0;
        int countP = 0, countT = 0;
        scanf("%s", c);
        for(int j=0;j<strlen(c);j++){
            if(c[j] != 'P' && c[j] != 'A' && c[j] != 'T'){
                result = false;
                break;
            }
            if(c[j] == 'P'){
                countP ++;
                if(countP == 2){
                    result = false;
                    break;
                }
            }
            if(c[j] == 'T'){
                countT ++;
                if(countT == 2){
                    result = false;
                    break;
                }
            }
        }
        if(result){
            string s = c;//需C++头文件下使用
            count1 = s.find("P");
            count2 = s.find("A");
            count3 = s.find("T");
            if(count1 == -1 || count2 == -1 || count3 == -1){
                result = false;
            }
            else{
                result = ((count1 * (count3 - count1 - 1)) == (strlen(c) - count3 - 1)) ? true : false;
            }
        }
        if(result){
            printf("YES\n");
        }
        else{
            printf("NO\n");
        }
    }
    return 0;
}

点评:string s类型定义需在C++环境下使用,不能再C语言环境下

 使用,这个需学会使用C++。

/*改进:
#include<stdio.h> 
#include<string.h>
#define N 10000
char inStr[N]; 
int len;
bool checkA(int begin,int end) {   
	for(int i=begin;i<end;i++)    
		if( 'A'!=inStr[i] )     
			return false;   
	return true; 
}

bool check() {   
	int i,posP,posT;   
	for(i=0;i<len;i++) {
	    if( 'P'==inStr[i] )     
			posP=i;
	    if( 'T'==inStr[i] )
		    posT=i;   
	}
	if( checkA(0,posP) && checkA(posP+1,posT) && checkA(posT+1,len) 
		&& posT-posP>1 && (posT-posP-1)*(posP)==len-posT-1 )    
		return true;   
	return false; 
}

bool checkOne() {
   for(int i=0;i<len;i++) 
      if( !('P'==inStr[i] || 'A'==inStr[i] || 'T'==inStr[i]) ) 
	      return false;   
	return true; 
}

int main() 
{   
	int nCases;   
	scanf("%d",&nCases);   
	while(nCases--){    
		scanf("%s",inStr);    
		len=strlen(inStr);    
		printf("%s\n",(checkOne() && check())?"YES":"NO");   
	}

  	return 0; 
}
再来,由题知: 
只存在'P', 'A', 'T'三种字符;
'P', 'T'只能出现一次并且按照该顺序先后出现;
P&T之间不能没有A;
T之后A的数量 = P之前A的数量 × P&T中间A的数量。
代码实现:
使用三个数count[3]记录P之前,P&T之间,T之后A的数量。
为了检测'P', 'T'的出现及次序,使用一个标记变量pos:
其值在出现P之前为0(使用count[0]记录P之前的A)
只有在出现P且其值为0时,将值变为1(使用count[1]记录P&T之间的A)
只有在出现T且其值为1时,将其变为2(使用count[2]记录T之后的A)
这样即可保证除此之外出现非'A'字符的情况都是不符合要求的。pos顺便还能作为count[]的索引。

 
#include <stdio.h>
int main()
{
    char c;
    int num;
    scanf("%d", &num);
    while(getchar() != '\n') ; //清除输入缓冲区的字符

    for(int i = 0; i < num; i++)
    {       
        int pos = 0, count[3] = {0, 0, 0};
        while((c = getchar()) != '\n')
        {
            if(c == 'A')                    count[pos]++;
            else if(c == 'P' && pos == 0)   pos = 1;       
            	 else if(c == 'T' && pos == 1)   pos = 2;        
            		  else                  break;         
        }

        if(c == '\n' && pos == 2                          
         && count[1] && count[2] == count[1] * count[0])    
            puts("YES");
        else
            puts("NO");

        if(c != '\n') 
			while((c = getchar()) != '\n'); //清除输入缓冲区的字符
    }
    return 0;
}

评:对字符认识深刻的使用

C++字符串解决:/*根据题意,假设b=A,根据第二条可得,c=a,所以可得aPbTc=aPbTa,而根据题意aPbATaa也是正确的,
根据观察可得,当P、T中间多了一个A时,T后面也会多一个a,所以可得P前面a的个数PT之间A的个数=T后面a的个数,
即P前面A的个数PT之间A的个数=T后面A的个数,同时PT之间至少要有一个A,当然,P也要在T之前。
*/ 


#include<iostream>
using namespace std;
int main(void)
{
    int n,a,b,c;
    string s;
    cin>>n;
    while(n--){
        cin>>s;
        int f=0;
        for(int i=0;i<s.size();i++){
            if(s[i]!='P'&&s[i]!='A'&&s[i]!='T'){
                f=1;
                break;
            }
            else{
                if(s[i]=='P') a=i;
                if(s[i]=='T') b=i;
            }
        }
        if(f) cout<<"NO\n";
        else{
            c=s.size()-b-1;
            b=b-a-1;
            if(a*b==c&&b) cout<<"YES\n";
            else cout<<"NO\n";
        }        
    }
    return 0;
}

 

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
如果您想读取.pat文件并添加内容,可以按照以下步骤操作: 1. 使用C++的fstream库打开.pat文件,例如: ```cpp #include <fstream> #include <iostream> int main() { std::ifstream file("example.pat"); if (file.is_open()) { // 文件成功打开,可以进行读取和写入操作 std::cout << "File opened successfully!\n"; } else { // 文件打开失败,输出错误信息 std::cerr << "Failed to open file!\n"; } return 0; } ``` 2. 读取文件内容并添加新内容,例如: ```cpp #include <fstream> #include <iostream> int main() { std::ifstream file("example.pat"); if (file.is_open()) { // 文件成功打开,可以进行读取和写入操作 std::cout << "File opened successfully!\n"; // 读取文件内容 std::string line; while (std::getline(file, line)) { std::cout << line << '\n'; } // 添加新内容 std::ofstream outfile("example.pat", std::ios::app); if (outfile.is_open()) { outfile << "New content\n"; std::cout << "New content added successfully!\n"; } else { std::cerr << "Failed to add new content!\n"; } } else { // 文件打开失败,输出错误信息 std::cerr << "Failed to open file!\n"; } return 0; } ``` 3. 关闭文件,例如: ```cpp #include <fstream> #include <iostream> int main() { std::ifstream file("example.pat"); if (file.is_open()) { // 文件成功打开,可以进行读取和写入操作 std::cout << "File opened successfully!\n"; // 读取文件内容 std::string line; while (std::getline(file, line)) { std::cout << line << '\n'; } // 添加新内容 std::ofstream outfile("example.pat", std::ios::app); if (outfile.is_open()) { outfile << "New content\n"; std::cout << "New content added successfully!\n"; } else { std::cerr << "Failed to add new content!\n"; } // 关闭文件 file.close(); outfile.close(); } else { // 文件打开失败,输出错误信息 std::cerr << "Failed to open file!\n"; } return 0; } ``` 这样您就可以读取.pat文件并添加新内容了。请注意,在添加新内容时,我们使用了std::ofstream库和std::ios::app参数来打开文件以追加内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值