1.简单 AC
while True: try: n=int(input()) T=input() flag=0 begin,end=len(T)-1,0 for i in range(len(T)): if flag==0 and T[i]=='M': flag=1 elif flag==1 and T[i]=='T': begin=i+1 break flag=0 for i in range(len(T)-1,-1,-1): if flag==0 and T[i]=='T': flag=1 elif flag==1 and T[i]=='M': end=i-1 break if begin<=end: print (T[begin:end+1]) else: print("") except: break2.也比较简单 AC
while True: try: n=int(input()) a=[] for i in range(n): a.append([int(s) for s in input().split()]) flag=[0]*n res=[] for i in range(n): for j in a[i]: if flag[j-1]==0: flag[j-1]=1 res.append(j) break print(' '.join([str(r) for r in res])) except: break3.这道题我是拓扑排序,看拓扑排序长度。一直是73% 可能是超时
while True: try: n,x,y=map(int,input().split()) edge=[] for i in range(n-1): uu,vv=map(int,input().split()) edge.append((uu,vv)) path={} for uu,vv in edge: if uu in path: path[uu].append(vv) else: path[uu]=[vv] if vv in path: path[vv].append(uu) else: path[vv]=[uu] print(path) res=-1 visited=[0]*(n+1) q=[x] while q: res+=1 q1=q q=[] for s in q1: for pq1 in path[s]: if visited[pq1]==0: visited[pq1]=1 q.append(pq1) print(res) except: break4、AC。我的思路是先找出每个数字的左右边界,然后l从1到m判断l是否合理,如果合理的话,r就从m开始遍历到l,找到符合要求的数量就ok了。
while True: try: m,n=map(int,input().split()) seq=list(map(int,input().split())) di={} for i in range(n): if seq[i] not in di: di[seq[i]]=[i,i] else: max_i,min_i=di.get(seq[i]) di[seq[i]]=[max(max_i,i),min(min_i,i)] res=0 for i in range(1,m+1): mi,ma=0,n flag=0 for j in range(1,i): if j not in di: continue max_j,min_j=di.get(j) if min_j>=mi: mi=max_j else: flag=1 break if flag==1:continue res+=1 for j in range(m,i,-1): if j not in di: res+=1 continue max_j,min_j=di.get(j) if max_j<=ma and min_j>=mi: res+=1 ma=min_j else: break print(res) except: break
全部评论
(3) 回帖