| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | 
| 12 | 13 | 14 | 15 | 16 | 17 | 18 | 
| 19 | 20 | 21 | 22 | 23 | 24 | 25 | 
| 26 | 27 | 28 | 29 | 30 | 31 | 
													Tags
													
											
												
												- DP
- 다익스트라
- String
- Brute Force
- Stored Procedure
- Hash
- Dijkstra
- MYSQL
- binary search
- union find
- two pointer
- Trie
- 그래프
- SQL
- 이진탐색
- 스토어드 프로시저
- Two Points
													Archives
													
											
												
												- Today
- Total
codingfarm
앞자리가 같은 숫자들 본문
programmers.co.kr/learn/courses/30/lessons/42577#
위 문제를 잘못 이해하고 풀었다.
특정 문자열이 주어질때, 앞자리가 같은 문자열이 존재하면 false 없으면 true를 반환해야한다.
가령
123456
123789
위 2개의 숫자는 앞자리 123이 일치하므로 false를 반환해야한다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | #include <string> #include <vector> #include <iostream> using namespace std; int GetChar(char ch) {     return ch - '0'; } class TrieNode { public:     bool isEnd, isContinue;     TrieNode* nodes[10];     TrieNode() {         for (int i = 0; i < 10; i++)             nodes[i] = 0;     }     void Clear() {         for (int i = 0; i < 10; i++) {             if (nodes[i] != 0) {                 nodes[i]->Clear();                 delete nodes[i];             }             nodes[i] = 0;         }     }     bool Insert(string str, int index) {         int ch = GetChar(str[index]);         bool result = true;         if (isEnd == true || isContinue == true)             return false;         if (nodes[ch] == 0)             nodes[ch] = new TrieNode();                 if (str.size() - 1 == index) {             isEnd = true;         }         else {             result = nodes[ch]->Insert(str, index + 1);             nodes[ch]->isContinue = true;         }         return result;     }     void Func(string& str) {         cout << str << endl;         for (int i = 0; i < 10; i++) {             if (nodes[i] != 0) {                 str.push_back(i + '0');                 nodes[i]->Func(str);                 str.pop_back();             }         }     } }; bool solution(vector<string> phone_book) {     int N = phone_book.size();     bool ret;     TrieNode dictionary;     dictionary.Clear();     for (int i = 0; i < N; i++) {         ret = dictionary.Insert(phone_book[i], 0);         if (ret == false)             break;     }     string str;     dictionary.Func(str);     dictionary.Clear();     return ret; } int main(void) {     vector<string> phone_book;     int T,N;     cin >> T;     string str;     for (int t = 0; t < T; t++) {         cin >> N;         for (int i = 0; i < N; i++) {             cin >> str;             phone_book.push_back(str);         }                 bool ret = solution(phone_book);         cout << ret << endl;         phone_book.clear();     }     return 0; } /* 4 3 119 97674223 1195524421 3 123 456 789 5 12 123 1235 567 88 1 12 */ /* 1 2 123456 123789 */ | cs | 
'Algorithm & Data structure > Problem Solution' 카테고리의 다른 글
| 백준 - 4485. 녹색 옷 입은 애가 젤다지? (0) | 2020.10.10 | 
|---|---|
| 백준 - 1238. 파티 (0) | 2020.10.10 | 
| 백준 - 1762. 평면그래프와 삼각형 (4) | 2020.05.16 | 
| 알고리즘 (0) | 2020.01.20 | 
| 945. Minimum Increment to Make Array Unique (0) | 2019.12.29 | 
			  Comments
			
		
	
               
           
					
					
					
					
					
					
				