PTA Speech Patterns

链接

https://pintia.cn/problem-sets/994805342720868352/problems/994805398257647616

题解

题目没啥好说的
只是它卡常数十分不爽
一开始我直接给一个带有 s t r i n g string string变量的结构体排序,结果 T T T
然后就开了一个 i n d e x index index数组
根据结构体中存储的内容,给 i n d e x index index数组排序,这样就快了很多
最后还是200ms+卡时限过的

代码

#include <bits/stdc++.h>
#define ll long long
#define cl(x) memset(x,0,sizeof(x))
#define maxn 1048999 
using namespace std;
char s[maxn];
struct word
{
	string w;
	ll cnt;
}ans, lis[600010];
ll tot, id[maxn];
bool cmp(ll a, ll b)
{
	if(lis[a].cnt!=lis[b].cnt)return lis[a].cnt>lis[b].cnt;
	return lis[a].w<lis[b].w;
}
bool operator<(word a, word b)
{
	if(a.cnt!=b.cnt)return a.cnt>b.cnt;
	return a.w<b.w;
}
void getline()
{
	char c;
	ll p=0;
	for(c=getchar();c!=10 and c!=-1;c=getchar())s[p++]=c;
	s[p++]=0x20;
}
void solve()
{
	string wd="";
	char *p;
	ll x=0, i;
	for(p=s;*p;p++)
	{
		if(isdigit(*p))wd+=*p;
		else if(isalpha(*p))wd+=tolower(*p);
		else
		{
			if(wd.length())
			{
				id[tot]=tot;
				lis[tot++]=(word){wd,1};
			}
			wd="";
		}
	}
	sort(id,id+tot,cmp);
	ans=lis[id[0]];
	for(i=1;i<tot;i++)
	{
		if(lis[id[i]].w==lis[id[i-1]].w)lis[id[i]].cnt=lis[id[i-1]].cnt+1;
		ans=min(ans,lis[id[i]]);
	}
	cout<<ans.w<<' '<<ans.cnt;
}
int main()
{
	getline();
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值