[SWEA/C++] 2007 패턴 마디의 길이

Hanbi·2022년 5월 23일
0

Problem Solving

목록 보기
21/108
post-thumbnail

문제

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5P1kNKAl8DFAUq&categoryId=AV5P1kNKAl8DFAUq&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=CCPP&select-1=2&pageSize=10&pageIndex=1

풀이

str, tmp 변수 선언하고, tmp에 하나씩 넣으면서
substr(tmp.size(), j) == tmp
tmp.size() //시작 위치
j //길이

코드

#include <iostream>
#include <string>

using namespace std;

int main() {
	int test_case;
	int T;

	cin >> T;
	for (test_case = 1; test_case <= T; ++test_case) {
		string str, tmp;

		cin >> str;
		tmp.push_back(str[0]);

		for (int i = 1; i < str.size(); i++) {
			if (tmp != str.substr(tmp.size(), i))
				tmp.push_back(str[i]);
			else
				break;
		}

		cout << "#" << test_case << " " << tmp.size() << endl;
	}

	return 0;
}
profile
👩🏻‍💻

0개의 댓글