๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š ์ „๊ณต ๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•ด์„ ๋ฐ ์„ค๊ณ„

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์‹คํŒจ ํ•จ์ˆ˜ - ์ค‘๋ณต ์ตœ๋Œ€ ๋ถ€๋ถ„๋ฌธ์ž์—ด(cpp)

728x90

์˜์–ด ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ์ตœ๋Œ€ ๊ธธ์ด 1,000์ธ ๋ฌธ์ž์—ด์—์„œ, ๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜์˜ค๋ฉด์„œ ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
์˜ˆ๋ฅผ ๋“ค์–ด, banana์˜ ๊ฒฝ์šฐ ๊ธธ์ด๊ฐ€ 5์ธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ banan, anana, ๊ธธ์ด๊ฐ€ 4์ธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ bana, anan, nana, ๊ธธ์ด๊ฐ€ 3์ธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ban, ana, nan, ana์ด๋ฏ€๋กœ ana๊ฐ€ 2๋ฒˆ ๋‚˜์˜ค๋ฉด์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธธ๋‹ค. ๋‹ต์€ ๋”ฐ๋ผ์„œ ์ด ๊ฒฝ์šฐ 3์ด ๋œ๋‹ค. 
ํžŒํŠธ: ์‹คํŒจํ•จ์ˆ˜๋ฅผ ์ž˜ ์ด์šฉํ•ด๋ณด์ž.

์ž…๋ ฅ
ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค. ์ž…๋ ฅ์€ ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ง€๋ฉฐ, ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 1,000์ธ ์˜์–ด ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด๋‹ค. 

์ถœ๋ ฅ
ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์ถœ๋ ฅ์€ ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ง€๋ฉฐ, ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์—์„œ ๋‘๋ฒˆ ์ด์ƒ ๋‚˜์˜ค๋ฉด์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. 

์˜ˆ์ œ ์ž…๋ ฅ 1
abcdefg
์˜ˆ์ œ ์ถœ๋ ฅ 1
0

 

๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜์˜ค๋ฉด์„œ ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ œ!

ํ•œ๋งˆ๋””๋กœ ๋งํ•˜๋ฉด ์ตœ๋Œ€ ํŒจํ„ด์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค.

์ˆ˜์—…์‹œ๊ฐ„์— ๋ฐฐ์šด ์‹คํŒจํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

 

์‹คํŒจํ•จ์ˆ˜(failure function)

์ •์˜ : f(i)๋Š” P[1..i]์—์„œ, ์ ‘๋‘์‚ฌ=์ ‘๋ฏธ์‚ฌ์ด๋ฉด์„œ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์˜ ๊ธธ์ด

  • โžข  ์ •์˜์— ์˜ํ•ด์„œ, 0 ≤ f(i) < i (๋‚˜์ค‘์— ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ฑ๋Šฅ ๋ถ„์„์—์„œ ์ค‘์š”ํ•˜๊ฒŒ ์“ฐ์ž„)
  • โžข  i+1๋ฒˆ์งธ ์œ„์น˜์—์„œ ๋งค์น˜๊ฐ€ ์‹คํŒจํ•˜๋ฉด, P[1..i]๊นŒ์ง€๋Š” ๋งค์น˜๊ฐ€ ์„ฑ๊ณตํ–ˆ๋‹ค๋Š” ๋œป
  • โžข  ์•ž ํŽ˜์ด์ง€ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด, ๋™์ผํ•œ ์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ์ผ์น˜ํ•˜๊ฒŒ ๋งŒ๋“ค๋ฉด ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ ๋น„๊ตํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. (์ถ”๊ฐ€๋กœ, ์ด ๋ถ€๋ถ„ ๋‹ค์Œ๋ถ€ํ„ฐ๋งŒ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค)
  • โžข  i - f(i)๋งŒํผ P๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.
์‹คํŒจํ•จ์ˆ˜

 

 

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    string banana;
    cin >> banana;

    int sameLen = banana.length() - 1;
    int searchLen = 2;
    bool escape = false;
    vector<int> sameLenList(banana.length() * banana.length());
    int listIndex = 0;

    while (!escape)
    {
        int start = 0;
        for (int k = 0; k < searchLen; ++k) {
            string search = "";
            for (int i = start; i < sameLen+start; ++i) {
                search = search + banana[i];
            }
            start++;
            string tmp = banana;
            
            if (banana.find(search) != string::npos)
            {
                tmp.erase(tmp.begin(), tmp.begin() + banana.find(search)+1);
                if (tmp.find(search) != string::npos)
                {
                    // cout << sameLen << "\n";
                    sameLenList[listIndex] = sameLen;
                    escape = true;
                    break;
                }
            }
            listIndex++;
        }
        sameLen--;
        searchLen++;
    }
    int result = *max_element(sameLenList.begin(), sameLenList.end());
    cout << result;
}

 

728x90