ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต 2019. 9. 20. 22:09
    728x90
    #include <string>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    bool solution(vector<string> phone_book) {
    
    	sort(phone_book.begin(), phone_book.end());
    
    	for (int i = 0; i < phone_book.size() - 1; ++i)
    	{
    		if (phone_book[i] == phone_book[i + 1].substr(0, phone_book[i].size()))
    			return false;
    	}
    
    	return true;
    }
    /*bool solution(vector<string> phone_book) {
    
    	for (vector<string>::iterator iter1 = phone_book.begin(); iter1 != phone_book.end(); ++iter1)
    	{
    		for (vector<string>::iterator iter2 = iter1 + 1; iter2 != phone_book.end(); ++iter2)
    		{
    			if ((*iter2).find(*iter1) == 0)
    				return false;
    		}
    	}
    	return true;
    }*/
    int main()
    {
    	vector<string> phone_book;
    	bool ans;
    
    	phone_book.push_back("119");
    	phone_book.push_back("97674223");
    	phone_book.push_back("1195524421");
    
    
    	ans = solution(phone_book);
    
    	cout << ans << endl;
    
    }

    ๋ฌธ์ œ ์„ค๋ช…

    ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ ์ค‘, ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.
    ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ, ๊ตฌ์กฐ๋Œ€ ์ „ํ™”๋ฒˆํ˜ธ๋Š” ์˜์„์ด์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค.

    • ๊ตฌ์กฐ๋Œ€ : 119
    • ๋ฐ•์ค€์˜ : 97 674 223
    • ์ง€์˜์„ : 11 9552 4421

    ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด phone_book ์ด solution ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์–ด๋–ค ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฉด false๋ฅผ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด true๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

    ์ œํ•œ ์‚ฌํ•ญ

    • phone_book์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 1,000,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • ๊ฐ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ์ œ

    phone_book return
    [119, 97674223, 1195524421] false
    [123,456,789] true
    [12,123,1235,567,88] false

    ์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

    ์ž…์ถœ๋ ฅ ์˜ˆ #1

    ์•ž์—์„œ ์„ค๋ช…ํ•œ ์˜ˆ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ #2

    ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ธ ๊ฒฝ์šฐ๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ๋‹ต์€ true์ž…๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ #3

    ์ฒซ ๋ฒˆ์งธ ์ „ํ™”๋ฒˆํ˜ธ, “12”๊ฐ€ ๋‘ ๋ฒˆ์งธ ์ „ํ™”๋ฒˆํ˜ธ “123”์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ต์€ false์ž…๋‹ˆ๋‹ค.

     

     

    728x90

    ๋Œ“๊ธ€

Designed by Tistory.