快手算法题A卷笔试题 80 100 60 100
第一题 推荐结果打散 C++ AC80
#include <iostream> #include <vector> #include <algorithm> #include <set> #include <math.h> #include <string> #include <queue> #include <stdlib.h> #include <stdio.h> #include <map> using namespace std; int main() { int N,M; string str; cin >> N >> M; vector<string>s; while(M--) { cin >> str; s.push_back(str); } vector<string>v; vector<string>p; vector<string>res; if(N>1) { for(int i=0;i<s.size();i++) { if(p.empty() && s[i][0] == 'V') res.push_back(s[i]); else if(p.empty() && s[i][0] == 'P') p.push_back(s[i]); else if(s[i][0] == 'V') v.push_back(s[i]); else p.push_back(s[i]); } if(v.empty() && !p.empty()) res.push_back(p[0]); int l = 0; int r = 0; while(l<v.size()) { if(r < p.size()) res.push_back(p[r++]); for(int i=0;i<N-1;i++) { if(l < v.size()) res.push_back(v[l++]); } } } else res = s; cout << res.size() << endl; for(int i=0;i<res.size();i++) cout << res[i] << endl; system("pause"); return 0; }第二题 无重复字符最长子串 C++ AC100
#include<bits/stdc++.h> using namespace std; int main() { map<char, int>hash; string str; cin >> str; for(int i=0;i<str.size();i++) { if(hash.find(str[i]) == hash.end()) hash[str[i]] = 0; } int l=0,r=0; int len = 0; while(r<str.size()) { if(hash[str[r]] == 0) hash[str[r++]]++; else { len = max(len, r-l); hash[str[l++]]--; } } cout << len << endl; return 0; }第三题 健身题 python3 AC60
第四题 解一元一次方程正整数解 python3 AC100N=int(input())dists=list(map(int, input().split(' ')))es=list(map(int, input().split(' ')))dists_es = list(zip(dists, es))dists_es.sort(key=lambda x:x[0])dp=[]for k in range(N):tmp=[-1 for i in range(N)]dp.append(tmp)weights=[2*dists_es[i][0]+dists_es[i][1] for i in range(N)]helper_max=[0 for i in range(N)]for i in range(N):if i==0:dp[0]=weightselse:tmp_max = dp[i-1][-1]helper_max[-1]=tmp_maxh=N-2while h>=0:tmp_max=max(tmp_max, dp[i-1][h])helper_max[h]=tmp_maxh-=1for j in range(N-i):dp[i][j]=dists_es[j][1]+helper_max[j+1]#max(dp[i-1][j+1:])print(max(dp[i]))
s=input() r=eval(s.replace('=','-(')+')',{'X':1j}) ans=0 flag=True try: ans = -r.real/r.imag except: print('-1') flag=False if flag: if ans%1==0 and int(ans)>0: print(int(ans)) else: print('-1')