PAT A1071 Speech Patterns (25分)测试点2错误

博主在解决PAT算法题A1071时遇到困难,尝试多种解决方案但测试点2仍报错,正在寻求问题所在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
在这里插入图片描述
难受,各种情况都试了,实在找不到哪里错了

#include <cstdio>
#include <iostream>
#include <string>
#include <map>

using namespace std;

map<string,int> m;

bool ischar(char c){
	if((c>='0'&&c<='9') || (c>='a'&&c<='z') || (c>='A'&&c<='Z')){
		return true;
	}
	return false;
}
int main(){
	string s;
	getline(cin,s);
	
	int len = s.size();
	int begin=-1, end=-1;
	for(int i=0; i<len; i++){
		if((ischar(s[i])&&i==0) || (ischar(s[i])&&!ischar(s[i-1]))){
			begin = i;
		}
		if((ischar(s[i])&&i==len-1) || (ischar(s[i])&&!ischar(s[i+1]))){
			end = i;
		}
		
		if(begin!=-1 && end!=-1){
			string sub = s.substr(begin, end-begin+1);
			if(sub[0]>='A' && sub[0]<='Z'){
				sub[0] = sub[0] + 32;
			}
			
			if(sub.length() != 0) m[sub]++;
	
			begin = -1;
			end = -1;
		}
	}
	
	int max = m.begin()->second;
	string temp = m.begin()->first;
	for(map<string,int>::iterator it=m.begin(); it!=m.end(); it++){
		if(it->second == max){
			if(it->first < temp){
				max = it->second;
				temp = it->first; 
			}
		} else if(it->second > max){
			max = it->second;
			temp = it->first;
		}
	}
	
	cout << temp << " " << max;
	
	return 0;
} 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值