欢迎关注更多精彩
关注我,学习常用算法与数据结构,一题多解,降维打击。
Codeforces Round 943 (Div. 3) 题解
题目列表:https://codeforces.com/contest/1968
A
https://codeforces.com/contest/1968/problem/A
题目大意
给定一个整数x, 找到y使得 gcd(x, y)+y最大(y<x)。gcd是最大公约数。
代码
直接枚举即可
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>
using namespace std;
int gcd(int a, int b) {
if (a % b == 0)return b;
return gcd(b, a % b);
}
void solve() {
int t;
cin >> t;
while (t--) {
int a;
cin >> a;
int ans = 0;
int y = 0;
for (int i = 1; i < a; ++i) {
int res = gcd(i, a) + i;
if (res >= ans) {
ans = res; y = i;
}
}
cout << y << endl;
}
}
int main() {
solve();
return 0;
}
/*
*/
B
https://codeforces.com/contest/1968/problem/B
题目大意
给定01串a,b, 问可以由b的子序列组成a最长前缀是多少。
解析
贪心算法判断。
依次遍历a, 寻找b中第一个相等的字符。
代码
代码中使用了二分查找,多此一举了。
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>
using namespace std;
class KMP {
vector<int> next;
string pattern;
public:
vector<int> makeNext(string pat);
vector<int> find(string s);
};
// a b a c a b a
// 0 0 1 0 1 2 3
vector<int> KMP::makeNext(string pat){
pattern = pat;
next.assign(pat.length(), 0);
for (int i=1, j=0; i < pattern.length(); i++) {
for (;j > 0 && pattern[j] != pattern[i];) {
j = next[j - 1];
}
if (pattern[j] == pattern[i]) {
j++;
}
next[i] = j;
}
return next;
}
vector<int> KMP::find(string s){
int j = 0;
vector<int> res;
for(int i=0;i< s.length();++i) {
char c = s.at(i);
for (;j > 0 && c != pattern[j];) {
j = next[j - 1];
}
if (c == pattern[j]) {
j++;
}
if (j == pattern.length()) {
res.push_back(i - pattern.length() + 1);
j = next[j - 1];
}
}
return res;
}
bool subsequence(string a, string b) {
int i, j;
for (i = 0, j = 0; i < a.length() && j < b.length(); ++j) if (a[i] == b[j])++i;
return i == a.length();
}
void solve() {
int t;
cin >> t;
KMP kmp;
while (t--<
Codeforces Round 943 (Div. 3) 题解

最低0.47元/天 解锁文章
1095

被折叠的 条评论
为什么被折叠?



