Codeforces Round 943 (Div. 3)

Codeforces Round 943 (Div. 3) 题解

欢迎关注更多精彩
关注我,学习常用算法与数据结构,一题多解,降维打击。

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--<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值