Commit a27e721 1 parent 83d3906 commit a27e721 Copy full SHA for a27e721
File tree 2 files changed +80
-6
lines changed
Algorithm/최장공통부분문자열_LongestCommonSubstring
2 files changed +80
-6
lines changed Original file line number Diff line number Diff line change @@ -9,10 +9,18 @@ def find(str1, str2):
9
9
10
10
for i in range (1 , len1 + 1 ):
11
11
for j in range (1 , len2 + 1 ):
12
- if str1 [i - 1 ] == str2 [j - 1 ]: table [i ][j ] = table [i - 1 ][j - 1 ]+ 1
13
- if max_comp < table [i ][j ]:
14
- max_comp = table [i ][j ]
15
- last_idx = i
12
+ if str1 [i - 1 ] == str2 [j - 1 ]:
13
+ table [i ][j ] = table [i - 1 ][j - 1 ]+ 1
14
+
15
+ if max_comp < table [i ][j ]:
16
+ max_comp = table [i ][j ]
17
+ last_idx = i
18
+
19
+ # print(table)
20
+ print (f" { ' ' .join (list (str2 ))} " )
21
+ for i , t in enumerate (table ):
22
+ print (str1 [i - 1 ] if i > 0 else " " , end = " " )
23
+ print (* t )
16
24
17
25
ans = ""
18
26
while max_comp :
@@ -23,7 +31,7 @@ def find(str1, str2):
23
31
print (ans )
24
32
25
33
if __name__ == "__main__" :
26
- str1 = "BCBBBC "
27
- str2 = "CBBBCC "
34
+ str1 = "BCBBBCADDDAA "
35
+ str2 = "CBBBCCCADDDAA "
28
36
29
37
find (str1 , str2 )
Original file line number Diff line number Diff line change
1
+ # 그래프 이론, 그래프 탐색, 너비 우선 탐색
2
+ # https://www.acmicpc.net/problem/9205
3
+
4
+ """
5
+ 2
6
+ 2
7
+ 0 0
8
+ 1000 0
9
+ 1000 1000
10
+ 2000 1000
11
+ 2
12
+ 0 0
13
+ 1000 0
14
+ 2000 1000
15
+ 2000 2000
16
+
17
+ happy
18
+ sad
19
+ """
20
+
21
+ input = __import__ ("sys" ).stdin .readline
22
+ t = int (input ())
23
+
24
+ def length (a : tuple , b : tuple ):
25
+ return abs (a [0 ]- b [0 ]) + abs (a [1 ]- b [1 ])
26
+
27
+ def solution ():
28
+ n = int (input ()) # store num
29
+ node = []
30
+ node .append (tuple (map (int , input ().split ())))
31
+ for _ in range (n ):
32
+ node .append (tuple (map (int , input ().split ())))
33
+ node .append (tuple (map (int , input ().split ())))
34
+
35
+ n_ = n + 2
36
+ festival_id = n + 1
37
+ max_range = 20 * 50
38
+ graph = [[] for _ in range (n_ )]
39
+
40
+ for i in range (n_ ):
41
+ for j in range (n_ ):
42
+ if i >= j or length (node [i ], node [j ]) > max_range :
43
+ continue
44
+ graph [i ].append (j )
45
+ graph [j ].append (i )
46
+
47
+ from collections import deque
48
+ queue = deque ([0 ])
49
+ visit = [False ] * n_
50
+
51
+ while queue :
52
+ node = queue .pop ()
53
+ visit [node ] = True
54
+
55
+ if node == festival_id :
56
+ return "happy"
57
+
58
+ for next in graph [node ]:
59
+ if visit [next ]:
60
+ continue
61
+ queue .append (next )
62
+
63
+ return "sad"
64
+
65
+ for _ in range (t ):
66
+ print (solution ())
You can’t perform that action at this time.
0 commit comments