※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 6993번 문제인 Shift Letters이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/6993 

 

6993번: Shift Letters

The first line in the test data file contains the number of test cases (< 100). After that, each line contains one test case, a word, w, (provided as a String) followed by an integer, n (int). You can assume that: 0 < n < length(w).

www.acmicpc.net

길이가 slen인 문자열을 n만큼 shift한 문자열은 (원래 문자열의 마지막 n개 문자로 구성된 문자열)와 (원래 문자열의 첫 slen-n개 문자로 구성된 문자열)을 이어붙여 얻는 문자열과 같음을 관찰하자.

 

위 관찰을 따라 반복문을 이용해 답을 출력하는 것으로 문제를 해결하자.

 

아래는 제출한 소스코드이다.

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

int T;
string s; int sft, slen;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> T;
	while (T--) {
		cin >> s >> sft;
		slen = s.length();
		cout << "Shifting " << s << " by " << sft << " positions gives us: ";
		for (int i = slen - sft; i < slen; i++) cout << s[i];
		for (int i = 0; i < slen - sft; i++) cout << s[i];
		cout << '\n';
	}

}
728x90

+ Recent posts