第一题,AC,有个坑是strip()
import re def judge(s): if not s: return False if len(s) > 20: return False # 判断首字母是否为字母 if not s[0].isalpha(): return False # 判断字符串是否由字母和数字组成 if not s.isalnum(): return False # 判断是否含有数字 numMatch = re.compile('[0-9]') if not numMatch.search(s): return False return True T = int(input()) for _ in range(T): s = input().strip() # 这里要strip,大坑耽误不少时间 if judge(s): print('Accept') else: print('Wrong')
第二题,AC,优先队列
from functools import total_ordering import heapq @total_ordering class order: def __init__(self, index, v, w): self.index = index self.get = (v + w * 2)* -1 def __eq__(self,other): if self.index==other.index: return True def __gt__(self,other): if self.get>other.get: return True elif self.get<other.get: return False else: if self.index>other.index: return True elif self.index<other.index: return False n, m = map(int, input().split()) heap = [] for i in range(n): v, w = map(int, input().split()) heapq.heappush(heap,order(i + 1, v, w)) res = [] for i in range(m): curr=heapq.heappop(heap) res.append(curr.index) res.sort() # 这里有个坑…需要排序…要把结果index按字典序输出…… print(' '.join(map(str, res)))
第三题,64%…复杂度比较高超时
class goods: def __init__(self,l,r,s): self.l,self.r,self.s=l,r,s n=int(input()) w=list(map(int,input().split())) take=map(int,input().split()) S=[0 for _ in range(n+1)] for i in range(1,n+1): S[i]=S[i-1]+w[i-1] del w start=goods(1,n,S[n]) temp=set() t=goods(0,0,0) temp.add(start) temp.add(t) for i in take: for j in temp: if i>=j.l and i<=j.r: curr=j temp.remove(curr) if curr.l<i: temp.add(goods(curr.l,i-1,S[i-1]-S[curr.l-1])) if curr.r>i: temp.add(goods(i+1,curr.r,S[curr.r]-S[i])) break MAX=max(temp,key=lambda x: x.s) print(MAX.s)
第四题,18%……不知道哪里出了什么问题,用的dfs+回溯
全部评论
(7) 回帖