[문제보기]

 2007. 패턴 마디의 길이

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

[풀이과정]

필자는 이중포문을 이용하여 패턴을 구하였다. 

if(s[i] == s[j]) 로 처음으로 일치하는 곳을 찾고, while()문을 통해 마디를 구해준다.

이 때, i_cnt가 j와 같은 숫자까지 올라갔으면 flag와 break;를 통해 이중 포문을 종료해준다.

[소스코드]

#include <iostream>
using namespace std;

int main()
{
    cin.tie(0);
    cout.sync_with_stdio(false);
	int test_case;
	int T;
	
	cin>>T;
	for(test_case = 1; test_case <= T; ++test_case){
        string s;
        cin >> s;

        bool flag = true;
        int cnt;
        for(int i=0; i<s.size() && flag; i++){
            for(int j=i+1; j<s.size() && flag; j++){
                if(s[i] == s[j]){
                    int i_cnt = i+1;
                    int j_cnt = j+1;
                    cnt = 1;
                    while(1){
                        if(s[i_cnt++] != s[j_cnt++]) break;
                        cnt++;

                        if(i_cnt == j){
                            flag = false;
                            break;
                        }
                    }
                }
            }
        }

        cout << "#" << test_case << " " << cnt << "\n";
	}
	return 0;
}

 

[해결 과정 중 실수한 부분]