接上期(https://www.luogu.com.cn/blog/matthew101130/post-2022-shu-ji-ji-xun-ce-shi-1-post):
Problem 5 (2-1)
题目链接:https://www.mfstem.org/p/1347
Background
Special for beginners, ^_^
Description
这个题目很简单,请你给出所有输入字符串的共同的前缀中最长的那个,如果没有,就输出 “-1”。
Format
Input
一行,若干个用空格隔开的长度小于500的字符串,仅包含小写字母,字符串个数不超过200。
Output
输出题目要求的结果。
Samples
输入数据 1
ab abc abcd输出数据 1
ab输入数据 2
ab cd ef输出数据 2
-1Limitation
1s, 1024KiB for each test case.
解:
看到这题,我的第一反应是暴力枚举,毕竟暴力出奇迹嘛这种题你不用枚举用什么?
#include <bits/stdc++.h>
using namespace std;
string a, b, c, ans;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> a >> b >> c;
for (int i = 0; i < a.length(); i++)
for (int j = i; j < a.length(); j++)
if ((~b.find(a.substr(i, j-i+1))) && (~c.find(a.substr(i, j-i+1))))
if (a.substr(i, j-i+1).length() > ans.length())
ans = a.substr(i, j-i+1);
if (ans != "")
cout << ans;
else
cout << -1;
return 0;
}分数是如下写法:
80 Wrong Answer看到这个分数,我一时百思不得其解:肿么肥四怎么回事??
直到有一天,我看到了如下字样:
一行,若干个用空格隔开的长度小于500的字符串,仅包含小写字母,字符串个数不超过200。
我这才反应过来,但是时间紧迫,没来得及修改代码。
先把老师的正确代码贴上:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
string longestCommonPrefix(vector<string>& strs) {
int len = 0, ct = strs.size();
while (1) {
for (int i = 0; i < ct; ++i) {
if (len==strs[i].size() || strs[i][len]!=strs[0][len])
return strs[0].substr(0, len);
}
++len;
}
return "";
}
int main() {
string s;
vector<string> strs;
while (cin >> s) strs.emplace_back(s);
s = longestCommonPrefix(strs);
if (s == "") cout << -1;
else cout << s;
return 0;
}基本思路相信大家也能看懂,就是从strs[第一个(0)]到strs[最后一个]枚举,看看与strs[0]是否匹配,否则就返回。
因为是前缀,所以不需要再往后进行判断。
Problem 6 (2-2)
Background
Special for beginners, ^_^
Description
差不多先生觉得任何事差不多就行了,比如让他比较两个长度不超过 100 的小写字母构成的字符串 s1, s2,他就觉得只要每个字母分别在 s1, s2中出现的次数之差的绝对值小于等于一个整数 k,就可以认为它们差不多相等啦!
下面请你来看看,对于给出的字符串,差不多先生的判断是什么?
Format
Input
输入多行
每行分别输入两个字符串 s1, s2,以及一个整数 k(1≤k≤10)
Output
对于每行输入,如果差不多先生会认为它们相等,就输出 Yes,否则输出 No。
Samples
输入数据 1
oooo zccz 4
oooo zccz 3
oooo zccz 2输出数据 1
Yes
No
NoHint 1
字符串 “oooo” 中有 4 个 ‘o’ ,但是 “zccz” 中有 0 个 ‘o’,两者之差为 4 。
Limitation
1s, 1024KiB for each test case.
解:
本来吧,这题也就罢了:
#include <bits/stdc++.h>
using namespace std;
const int CHARN = (1<<7), N = 18;
#define mems(x) memset(x, 0, sizeof x)
char a[N], b[N];
int cnta[CHARN], cntb[CHARN], k, n, m;
int main() {
while (~scanf("%s%s%d", a+1, b+1, &k)) {
int n = strlen(a+1), m = strlen(b+1);
mems(cnta), mems(cntb);
for (int i = 1; i <= n; i++)
cnta[a[i]]++;
for (int i = 1; i <= m; i++)
cntb[b[i]]++;
bool flag = true;
for (int i = 'a'; i <= 'z'; i++)
if (abs(cnta[i]-cntb[i]) > k) {
puts("No");
flag = false;
break;
}
if (flag)
puts("Yes");
}
return 0;
}谁知道,我在
const int CHARN = (1<<7), N = 18;这一行少打了一个0
造成了
80 Wrong Answer的《悲惨世界》
不得不说,看题一定要细心
#include <bits/stdc++.h>
using namespace std;
const int CHARN = (1<<7), N = 18;
#define mems(x) memset(x, 0, sizeof x)
char a[N], b[N];
int cnta[CHARN], cntb[CHARN], k, n, m;
int main() {
while (~scanf("%s%s%d", a+1, b+1, &k)) {
int n = strlen(a+1), m = strlen(b+1);
mems(cnta), mems(cntb);
for (int i = 1; i <= n; i++)
cnta[a[i]]++;
for (int i = 1; i <= m; i++)
cntb[b[i]]++;
bool flag = true;
for (int i = 'a'; i <= 'z'; i++)
if (abs(cnta[i]-cntb[i]) > k) {
puts("No");
flag = false;
break;
}
if (flag)
puts("Yes");
}
return 0;
}思路就是说,用两个数组统计s1和s2中字符出现的数量,套上abs()后相减判差就行。
Problem 3 (2-3)
Background
Special for beginners, ^_^
Description
赌王HSK被捕了,原因是他开设了一个线上赌局,猜大小,规则是这样的:
每个人可以在1∼10^9以内随意选中一个数字,然后猜一个大小(大或小),0 作为小,1 作为大。
赌王开局,给出一个数字 x(0≤x≤10^9),规定所有不超过 x 的数都称为小,其余称为大,然后所有猜对的人就可以拿回自己的赌金,同时拿回一定数量的的奖金。
当然,这个赌局也有亏本的风险!所以,赌王做了一个算法,可以通过选择 x 使猜对的人数最少。
公安机关找到了算法大神的你,请教一下,这样的算法是怎样实现的呢?
Format
Input
第一行一个整数 n(1≤n≤2×10^5),表示这一轮有多少玩家
第二行 n 个整数,表示每位玩家选中的数字
第三行输入一个长度为 n 的 01 字符串,表示每位玩家猜的大小
Output
输出一个整数,表示猜对的人的数量。
Samples
输入数据 1
5
90 130 2 10000 10
10111输出数据 1
1Hint
x=90,此时只有第 4 位玩家猜对
输入数据 2
5
90 130 2 10000 10
00000输出数据 2
0输入数据 3
5
90 130 2 10000 10
11111输出数据 3
0Limitation
1s, 1024KiB for each test case.