牛客Round76(补题)

牛客Round76(补题)

B串串香

https://ac.nowcoder.com/acm/contest/99990/B

思路

容易想到的是构造出来的子串肯定是越短越好,那么最短的时候是长度为1,所以本题就是让你统计出现次数最多的字符。

评述

又是及其傻×的一天

代码

#include <bits/stdc++.h>
typedef std::pair<long long, long long> pll;
typedef std::pair<int, int> pii;
#define INF 0x3f3f3f3f
#define MOD 998244353
using i64 = long long;
const int N = 1e5+5;
void solve(){
int n;
std::cin >> n;
std::array<int, 26> aa;
std::fill(aa.begin(), aa.end(), 0);
for (int i = 0; i < n; i++){
char a;
std::cin >> a;
aa[a-'a']++;
}
int pos = -1, cnt = 0;
for (int i = 0; i < 26; i++){
if (aa[i] > cnt){
cnt = aa[i];
pos = i;
}
}
std::cout << pos << '\n';
std::cout << char(pos+'a') << '\n';
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout<<std::setiosflags(std::ios::fixed)<<std::setprecision(2);
int t = 1, i;
for (i = 0; i < t; i++){
solve();
}
return 0;
}

C小红的gcd

https://ac.nowcoder.com/acm/contest/99990/C

思路

首先要明确的是用gcd得到的新值替换旧的值这个事情肯定是不增的,所以一个序列的最终一定会全部等于一个定值,并且这个定值至多是原本序列的最小值。

gcd算法是有结合性的,gcd(gcd(a, b) ,gcd(c, d)) = gcd(a, b, c, d)。所以选择哪两个数先做gcd这件事是没有意义的,直接全部做gcd即可。

评述

对于题目中出现的性质一定要想清除,每次都是简单的想一下,模模糊糊觉得是错的就直接跳过了,结果跳过了真理。

代码

#include <bits/stdc++.h>
typedef std::pair<long long, long long> pll;
typedef std::pair<int, int> pii;
#define INF 0x3f3f3f3f
#define MOD 998244353
using i64 = long long;
const int N = 1e5+5;
void solve(){
int n;
std::cin >> n;
i64 gcd = 0;
for (int i = 0; i < n; i++){
int a; std::cin >> a;
gcd = std::gcd(a, gcd);
}
std::cout << 1LL * gcd * n << '\n';
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout<<std::setiosflags(std::ios::fixed)<<std::setprecision(2);
int t = 1, i;
for (i = 0; i < t; i++){
solve();
}
return 0;
}

本文作者:califeee

本文链接:https://www.cnblogs.com/califeee/p/18667425

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   califeee  阅读(12)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.