Skip to content

Conversation

@hadongun
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

IOIOI

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

47min

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

image

์ง€๋‚˜๊ฐ€๋‹ค ๋ณด์ธ ๋ฌธ์ž์—ด ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ์—์„œ ๋‚˜์˜จ P(N) ์ˆ˜์—ด์€ I๊ฐ€ ํ™€์ˆ˜ ์ธ๋ฑ์Šค์—, O๋Š” ์ง์ˆ˜ ์ธ๋ฑ์Šค์— ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์„ ๊ธฐ์–ตํ•ด์ฃผ์‹ญ์‹œ์˜ค

์ฃผ์–ด์ง„ ์ˆ˜์—ด์—์„œ P(N) ์ˆ˜์—ด์ด ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”๊ฐ€! ๋ฅผ ๊ตฌํ•ด์•ผํ•ด์š”

์ฃผ์–ด์ง„ ์ˆ˜์—ด s๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋ณต๋ฌธ while์— ์กฐ๊ฑด( i < ์ „์ฒด ๊ธธ์ด - p(n)๊ธธ์ด - 1 )์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹น
์œ„ ์กฐ๊ฑด๋ณด๋‹ค i๊ฐ€ ์ปค์ง€๋ฉด ๋” ์ด์ƒ P(N) ์ˆ˜์—ด์ด ๋“ฑ์žฅ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์•„๋ฌดํŠผ! ์œ„์—์„œ ๋งํ–ˆ๋˜ I, O๊ฐ€ ๋‚˜์˜ค๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์–ตํ•˜์—ฌ
I๋Š” [i + 2j + 1], O๋Š” [i + 2j + 2] ์œ„์น˜์— ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

P(N) ์ˆ˜์—ด์—๋Š” I๊ฐ€ N+1, O๊ฐ€ N๊ฐœ๊ฐ€ ์ด ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.
์ฒ˜์Œ์— I๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด ์ดํ›„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ง„์ž…ํ•˜๋„๋ก ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. -> ๋‚จ์€ I, O ๋“ฑ์žฅ ๊ฐœ์ˆ˜๋Š” ๊ฐ๊ฐ N, N
์•„๋ž˜ ๋ฐ˜๋ณต๋ฌธ์ด ์ด N๋ฒˆ ๋ฐ˜๋ณตํ•˜๋„๋ก ํ•˜์—ฌ I๋Š” [i + 2j + 1], O๋Š” [i + 2j + 2] ์ด s์ˆ˜์—ด ์ธ๋ฑ์Šค์— ๋งž๊ฒŒ ๋“ฑ์žฅํ•˜๋Š”์ง€ ํ™•์ธ.
๋“ฑ์žฅํ•˜์ง€ ์•Š์œผ๋ฉด is_pass = False๋กœ ๋ฐ”๊พธ์–ด ๋ฐ”๋กœ ๋ฐ˜๋ณต์„ ๋ฉˆ์ถ”๊ณ  ์นด์šดํŠธ ํ•˜์ง€ ์•Š๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜์—ฌ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์„ธ์–ด ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹น

ๆฐดๅฐŽcode

์ž…๋ ฅ N ๋ฐ›๊ธฐ
์ž…๋ ฅ s_len ๋ฐ›๊ธฐ
์ž…๋ ฅ ๋ฌธ์ž์—ด s๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•˜๊ธฐ

i๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ
count๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ

i๊ฐ€ s_len - 2*N ๋ณด๋‹ค ์ž‘์„ ๋™์•ˆ ๋ฐ˜๋ณตํ•˜๊ธฐ:
    ๋งŒ์•ฝ s[i]๊ฐ€ 'I'์ด๋ฉด:
        is_pass๋ฅผ True๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ
        j๋ฅผ 0๋ถ€ํ„ฐ N-1๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ธฐ:
            ๋งŒ์•ฝ s[i + 2*j + 1]์ด 'O'๊ฐ€ ์•„๋‹ˆ๋ฉด:
                is_pass๋ฅผ False๋กœ ๋ฐ”๊พธ๊ณ  ๋ฐ˜๋ณต๋ฌธ ๋‚˜๊ฐ€๊ธฐ
            ๋งŒ์•ฝ s[i + 2*j + 2]๊ฐ€ 'I'๊ฐ€ ์•„๋‹ˆ๋ฉด:
                is_pass๋ฅผ False๋กœ ๋ฐ”๊พธ๊ณ  ๋ฐ˜๋ณต๋ฌธ ๋‚˜๊ฐ€๊ธฐ
        ๋ฐ˜๋ณต์ด ๋๋‚œ ํ›„:
            ๋งŒ์•ฝ is_pass๊ฐ€ True์ด๋ฉด:
                count๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ
                i๋ฅผ 2 ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ
            ์•„๋‹ˆ๋ฉด:
                i๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ
    ์•„๋‹ˆ๋ฉด:
        i๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ

count ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜คํ˜ธ ์ƒ๊ฐ๋ชปํ•œ ํฅ๋ฏธ๋กœ์šด ํ’€์ด๋„ค์š”!
ํ•˜์ง€๋งŒ N์ด๋‚˜ M์ด ์ปค์ง€๋ฉด, ์„œ๋ธŒํƒœ์Šคํฌ 2์—์„œ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ ๊ฐ™์•„์š”.

์ €๋Š” ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ I์ธ ๊ฒฝ์šฐ ๋’ค์— OI๊ฐ€ ๋ช‡๊ฐœ์”ฉ ์˜ค๋Š”์ง€ ์นด์šดํŠธ ํ–ˆ์Šต๋‹ˆ๋‹ค.
P(N)์€ O๊ฐ€ N๊ฐœ ๋ผ๊ณ ๋„ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ฒซ๊ธ€์ž I๋ฅผ ์ œ์™ธํ•˜๋ฉด OI๊ฐ€ N๊ฐœ ๋ผ๊ณ ๋„ ํ•  ์ˆ˜ ์žˆ์”๋‹ˆ๋‹ค!!!!

OI์˜ ๊ฐœ์ˆ˜๊ฐ€ N๊ณผ ์ผ์น˜ํ•˜๋ฉด ๋‹ค์Œ ๋ฌธ์ž์— ๋˜ OI๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด
์ •๋‹ต์— ๊ฒฝ์šฐ์˜์ˆ˜ 1์„ ๋”ํ•˜๊ณ  ํ˜„์žฌ OI์นด์šดํŠธ๋ฅผ 1๋นผ์ค๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, P(4) = IOIOIOIOI ์ธ ๋ฌธ์ž์—ด์—์„œ, P(3)์„ ๊ตฌํ•ด์•ผ ํ•  ๋•Œ,
IOIOIOI + OI ์™€ IO + IOIOIOI๋กœ ๋ฐ”๋ผ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ๊ฒฝ์šฐ์—์„œ ์ •๋‹ต์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค๊ณ  ํ˜„์žฌ ์นด์šดํŠธ๋ฅผ ์‰ฝ์‚ฌ๋ฆฌ ์ดˆ๊ธฐํ™”ํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒƒ์ด์ฃ ...

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

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    
	int N, M;
    string S;
	cin >> N >> M >> S;
	
	int answer = 0, OIcount = 0;
	for(int i = 0; i < M; i++) {
	    if(S[i] == 'O') continue;
	    else {
	        while(S[i+1] == 'O' && S[i+2] == 'I') {
	            OIcount++;
	            if(OIcount == N) {
	                answer++;
	                OIcount--;
	            }
	            i += 2;
	        }
	    }
	    OIcount = 0;
	}
	
	cout << answer << '\n';
	return 0;
}

๋™์œค๋‹˜๋„ 100์ ์„ ํ–ฅํ•ด ๋„์ „~!!!!!

@froglike6
Copy link
Collaborator

์•ž์„œ ์ž˜ ๋ง์”€ํ•ด์ฃผ์…จ๋‹ค์‹œํ”ผ ๋™์œค๋‹˜์˜ ์ฝ”๋“œ๋Š” $\mathcal{O}(M\times N)$์ด๋ผ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ €๋Š” while์„ ํ†ตํ•ด 3๊ฐœ์˜ ๋ฌธ์ž๋งŒ ๋ณด๊ณ  ํŒ๋‹จํ•˜๋„๋ก ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

import sys
input = sys.stdin.readline

n = int(input())
m = int(input())
s = input().rstrip()

cnt, ans, i = 0, 0, 0

while i<m-2:
    if s[i]=="I" and s[i+1]=="O" and s[i+2]=="I":
        cnt+=1
        if cnt >= n:
            ans+=1
        i+=2
    else:
        cnt=0
        i+=1
print(ans)

@Fnhid
Copy link
Collaborator

Fnhid commented Jul 14, 2025

์ €๋Š” kmp๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!
๋ญ”๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์•„์„œ ํ•ด๋ดค๋Š”๋ฐ ๊ฐ€๋Šฅํ•˜๊ตฐ์š” ์—ญ์‹œ kmp

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<int> getPi(string &pattern) {
  int m = pattern.size();
  vector<int> pi(m, 0);
  int j = 0;

  for (int i=1;i<m;++i) {
    while (j > 0 && pattern[j] != pattern[i]) j = pi[j-1];
    if (pattern[j] == pattern[i]) pi[i] = ++j;
  }
  return pi;
}

int kmp(string &pattern, string &text) {
  int cnt=0;
  vector<int> pi = getPi(pattern);
  int n=text.size(), m=pattern.size(), j=0;
  for (int i=0;i<n;i++) {
    while (j>0 && pattern[j] != text[i]) j = pi[j-1];
    if (pattern[j] == text[i]) {  // pattern matching success
      if (j == m - 1) {
        cnt++;
        j = pi[j];
      } else {
        j++;
      }
    }

  }
  return cnt;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int n, m, nCount, j;
  string s, patt="I";
  cin >> n >> m >> s;

  for (int i =0;i<n;i++) {
    patt+="OI";
  }
  cout << kmp(patt, s);
  return 0;
}

@Fnhid Fnhid merged commit 19ec1e5 into AlgoLeadMe:main Jul 14, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants