快手算法题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
	
	
N=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]=weights
else:
tmp_max = dp[i-1][-1]
helper_max[-1]=tmp_max
h=N-2
while h>=0:
tmp_max=max(tmp_max, dp[i-1][h])
helper_max[h]=tmp_max
h-=1
for 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]))
第四题 解一元一次方程正整数解 python3 AC100
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')





#快手##笔试题目##题解#
全部评论
你这第四题写法和刚刚一个人一样……而且前面C++后面python,怕不是……
点赞 回复 分享
发布于 2019-08-25 19:08
第四题竟然用Python竟然能如此简单,用java写了几十行代码,给跪了。
点赞 回复 分享
发布于 2019-08-25 19:26
Eval用法有什么比较好的解析文章嘛?
点赞 回复 分享
发布于 2019-08-25 20:43
第三题的思路是啥?
点赞 回复 分享
发布于 2019-08-25 23:13

相关推荐

02-09 13:09
长安大学 Java
黑皮白袜臭脚体育生:简历条例统一按使用了什么技术实现了什么功能解决了什么问题或提升了什么性能指标来写 可以看看我帖子简历写法
点赞 评论 收藏
分享
评论
点赞
16
分享

创作者周榜

更多
牛客网
牛客企业服务