1+ const [ Ain , Bin ] = require ( 'fs' ) . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : require ( 'path' ) . resolve ( __dirname , '../../../testcase.txt' ) )
2+ . toString ( )
3+ . trim ( )
4+ . split ( '\n' )
5+ . map ( e => Array . from ( e ) )
6+
7+ function getLCS ( A , B ) {
8+ let result = [ ]
9+ for ( let start = 0 ; start < A . length ; start ++ ) {
10+ const temp = [ ]
11+ let last = 0
12+ // console.log('start',start)
13+ for ( let i = start ; i < A . length ; i ++ ) {
14+ for ( let j = last ; j < B . length ; j ++ ) {
15+ if ( A [ i ] === B [ j ] ) {
16+ temp . push ( A [ i ] )
17+ last = j + 1
18+ // console.log(A[i],i,B[j],j)
19+ i ++
20+ }
21+ }
22+ }
23+ if ( temp . length > result . length ) result = temp
24+ }
25+
26+ return result
27+ }
28+
29+ if ( Ain === undefined || Bin === undefined ) {
30+ return console . log ( 0 )
31+ }
32+
33+ const [ A , B ] = Ain . length < Bin . length ? [ Ain , Bin ] : [ Bin , Ain ]
34+
35+ let result = getLCS ( A , B )
36+ if ( A . length === B . length ) {
37+ const tempResult = getLCS ( B , A )
38+ if ( tempResult . length > result . length ) result = tempResult
39+ }
40+ console . log ( result . length )
41+ if ( result . length > 0 ) console . log ( result . join ( '' ) )
42+
43+
44+ // 00:43:48 틀렸습니다 -> last 로직 추가
45+ // 00:47:04 틀렸습니다 -> 길이 같을 때 예외처리
46+ // 00:59:07 틀렸습니다 -> 3중 포문으로 변경
47+ // 01:07:45 틀렸습니다 -> 버그 수정
48+ // 01:08:54 틀렸습니다 -> 길이 출력 빠져있었음
49+ // 01:17:22 틀렸습니다 -> last=>last+1로 변경
50+ // 01:20:22 틀렸습니다
51+ // 01:26:33 포기
52+
53+
54+
55+
56+ // const positions = {}
57+
58+ // for(let i=0;i<A.length;i++){
59+ // const char = A[i]
60+ // if(positions[char] === undefined){
61+ // positions[char]=[i]
62+ // }else{
63+ // positions[char].push(i)
64+ // }
65+ // }
66+
67+ // console.table(positions)
68+
69+ // for(let start=0;start<B.length;start++){
70+ // const tempPoistions = JSON.parse(JSON.stringify(positions))
71+ // const result = []
72+ // for(let i=start;i<B.length;i++){
73+ // const char = B[i]
74+ // if(Array.isArray(tempPoistions[char]) && tempPoistions[char].length >0){
75+ // // if(result.length !== 0 && result[result.length-1] > tempPoistions[char][0]) continue
76+ // result.push(tempPoistions[char].shift())
77+ // }
78+ // }
79+ // console.log(result)
80+ // console.log(getMaxSequence(result))
81+ // }
82+
83+ // function getMaxSequence(arr){
84+ // const dp = Array.from(arr,()=>[])
85+ // dp[0] = [arr[0],arr[0]]
86+ // for(let i=1;i<arr.length;i++){
87+
88+ // }
89+ // }
0 commit comments