글

라벨이 코딩문풀메모인 게시물 표시

Hashing 15829

이미지
 Hashing 15829 스트리크 깨질까봐 브론즈 하나 함. 교육적인 문제 해시 함수 하나 알려줌 해시 충돌을 피하기 위해 서로수인 숫자로 이런걸한다고 작은 수를 계수로 두고 스트링별로 배정된 수에 곱해서 합한다고.. <img alt="" data-original-height="142" data-original-width="422" height="108" src="https://blogger.googleusercontent.com/img/a/AVvXsEiy6Xx6etSIIan5AQuz51liLb7BW38McWIOrzSedwajy21mo8n3SkWfkg8o1KSH7DJ0Bbv1gsX1gWfIA9Lmj8FuuP-3uGkV41TNbT4VJVVe5Gjgo0K33NSCf9DHRgCJ-aXNIXv0K08xGjRZLBGh61hUe0ihi_33hWN7Kmg7uZ4SWXTPlVJcWcTUTJYelY0t" width="320" /> ```python N=int(input()) string=input() hash_dict={} for i in range(97,123):     hash_dict[chr(i)]=i-96 total=0 for i in range(N):     total+=31**i*hash_dict[string[i]] print(total%1234567891) ```

프린터 큐 1966

프린터 큐 1966 https://www.acmicpc.net/problem/1966   어렵진 않은데 좀 헷갈림 리스트 쓰지 말걸 그랬나 연습겸 ```python #1966 testcase=int(input()) for _ in range(testcase):     N,M=map(int,input().split())     docs_importance=list(map(int,input().split()))     tmp=[x for x in range(N)]     count=0     while len(tmp)>0:         if docs_importance[tmp[0]]>=max(docs_importance):             count+=1             if tmp[0]==M:                 print(count)                 break             docs_importance[tmp[0]]=0             tmp.pop(0)         else:             pop_idx=tmp.pop(0)             tmp.append(pop_idx)          ```

LCS 9251

이미지
LCS 9251 https://www.acmicpc.net/problem/9251  dp문제 였다. 그것도 모르고 set으로 풀다가 메모리 초과남. 이렇게 간편하게 될줄 알았는데.. 오산이고 ```python tmp1=input() tmp2=input() subsets_tmp1=set([""]) for i in range(len(tmp1)):     subsets_tmp1.update(list(map(lambda x: x+tmp1[i],subsets_tmp1)))      subsets_tmp2=set([""]) for i in range(len(tmp2)):     subsets_tmp2.update(list(map(lambda x: x+tmp2[i],subsets_tmp2))) subsets_inter=subsets_tmp1.intersection(subsets_tmp2) print(len(max(subsets_inter,key=len))) ``` dp로 이렇게 풀어서 됬는데 이차원 배열로 비교하다가 같으면 그 대각선꺼 +1 다르면 위 왼쪽꺼 비교이다. 이런 식 ```python # 9251 str1=input() str2=input() dp=[[0 for i in range(len(str1))] for j in range(len(str2))] samefound=False for i in range(len(str1)):          if str1[i]==str2[0]:         samefound=True     if samefound:         dp[0][i]=1 samefound=False for j in range(len(str2)):          if str1[0]==str2[j]: ...

덩치 7568

## 덩치 7568  https://www.acmicpc.net/status?from_mine=1&problem_id=7568&user_id=gongboo 문제 조건 널널해서 그냥 건성으로 싹다 비교하는걸로 풀음 ```python def a_is_smaller(a,b):     if a[0]<b[0] and a[1]<b[1]:         return True     return False n=int(input()) people=[] answer=[] for i in range(n):     people.append(list(map(int,input().split()))) for personA in people:     ans=0     for personB in people:         if a_is_smaller(personA,personB):             ans+=1     answer.append(ans) print(" ".join(list(map(lambda x: str(x+1),answer)))) ```

균형잡힌 세상 4949

 균형잡힌 세상 4949 https://www.acmicpc.net/problem/4949 ```python while True:     input_string=input()     if input_string==".":         break     stack=[]     for idx,character in enumerate(input_string):         if idx==len(input_string)-1:             if len(stack)>0:                 print("no")             else:                 print("yes")             break         if character in "[]()":             stack.append(character)             if stack[-2:]==["[","]"] or stack[-2:]==["(",")"]:                 stack.pop()                 stack.pop() ``` ## js로도 풀었다. ```javascript let d...

용액 2467

 용액 2467 https://www.acmicpc.net/problem/2467 아래와 같이 풀면 안됨 완전 착각해버림 하나씩 돌아가면서 그 수에 맞는 수를 투포인터 이분탐색으로 찾아가면서 하면되는 건줄알았는데. 실제로 그렇게 해서 풀었는데 그게 아니라 이분탐색 투포인터하는 과정중에 찾아지는 거였다. 그냥 불필요한 과정을 더 한것. ```python import math N=int(input()) liquids=list(map(int,input().split())) pointer_a=0 pointer_b_r=pointer_a+1 pointer_b_l=len(liquids)-1 pointer_b=pointer_b_l best_a,best_b=pointer_a,pointer_b for i in range(0,len(liquids)-1):     pointer_a=i     pointer_b_l=pointer_a+1     pointer_b_r=len(liquids)-1     pointer_b=pointer_b_l     while pointer_b_l<pointer_b_r:         mid=int((pointer_b_r+pointer_b_l)/2) #         print(pointer_a,pointer_b_l,pointer_b_r,mid)         if pointer_b_l+1==pointer_b_r:             if abs(liquids[pointer_a]+liquids[pointer_b_l])<abs(liquids[pointer_a]+liquids[pointer_b_r]):             ...

수 나누기 게임 27172

수 나누기 게임 27172 https://www.acmicpc.net/problem/27172 바보같이 나누기로 접근하다가 안돼서 배수로 접근 했더니 됐다 잘못된 접근방식으로 개고생, 왜 진작 이렇게 안했을까 사실 어렴풋하게 생각은 났는데 귀찮았다. 조금만 생각해도 이쪽이 배수 쪽이 더 가짓수가 적은데 왜 그랬을까 ```python import math N=int(input()) players=list(map(int, input().split())) player_set=set(players) scores_dict={x:0 for x in players} max_player=max(player_set) for player in players:     for i in range(1,math.ceil(max_player/player)+1):         if player*i in player_set:             scores_dict[player]+=1             scores_dict[player*i]-=1 for i in players[:-1]:     print(scores_dict[i],end=" ") print(scores_dict[players[-1]]) ```

다각형의 면적 2166

 https://www.acmicpc.net/problem/2166 다각형의 면적을 구하는데 고등학교때 기하와 벡터 배울때 한 거 떠올려서 풀었다. 신발끈 공식. 오랜만에 생각해본다. 공식만 알면 그냥 풀리는 골드 중에서 제일 쉬운 문제 아닐까   ```python N=int(input()) points=[] for i in range(N):     x,y=map(int,input().split())     points.append((x,y)) a1,a2=points[0][0],points[0][1] area=0 for i in range(1,N-1):     b1,b2=points[i][0],points[i][1]     c1,c2=points[i+1][0],points[i+1][1]     area+=(a1*b2+b1*c2+c1*a2-(a2*b1+b2*c1+c2*a1))/2 print(round(abs(area),1)) ``` 하다가 오목한 도형일경우 빼줘야 하는 경우 때문에 전체 계산후 abs를 해야 했는데 그 부분을 고려 안해서 조금 고민했다. 관련해서 찾아보다가 볼록 껍질이라는 개념이 있다고 해서 흥미로워서 덧붙여둠. --- # 볼록껍질 관련 설명 볼록껍질(Convex Hull) 알고리즘은 2차원 평면 위에 주어진 점들 중에서 이 점들을 모두 포함하는 가장 작은 볼록 다각형을 찾는 문제를 해결하기 위한 알고리즘입니다. 쉽게 말해, 주어진 점들 중 가장 바깥쪽에 위치한 점들을 연결하여 만들어진 다각형을 찾는 것입니다. ### 볼록껍질의 정의 볼록껍질은 다음과 같은 성질을 가집니다: - 볼록 다각형: 이 다각형의 각 내부각은 180도보다 작으며, 다각형의 내부에 있는 임의의 두 점을 잇는 선분이 항상 다각형 내부에 위치합니다. - 주어진 점 집합을 모두 포함하는 최소의 다각형입니다. ### 볼록껍질 알고리즘의 종류 여러 가지 알고리즘이 있지만, 대표적...

스택 수열 1874

  스택 수열  1874 https://www.acmicpc.net/problem/1874 난독을 불러일으키는 문제 스택에 12345.. 순서로 Push하거 스택 Pop할 수 있는데 이 방법으로 주어진 수열을 만들 수 있냐는 문제였다. ```python #1874 n=int(input()) num_lst=[] for i in range(1,n+1):     num_lst.append(int(input())) # num_lst.reverse() push_pop_lst=[] stack=[] increasing_num=1 is_fail=False for num in num_lst: #     print(num, stack,push_pop_lst)     if len(stack)==0:         stack.append(increasing_num)         push_pop_lst.append("+")         increasing_num+=1     while stack[-1]!=num:         if increasing_num>n:             is_fail=True             break         push_pop_lst.append("+")         stack.append(increasing_num)         increasing_num+=1          if is_fail:       ...