반응형

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

 

 

#문제 간단 정리

 

문자열을 사용한 구현문제정도

 

#문제 해결 방법

 

다른사람들은 어떻게 풀었을지 모르겠지만

 

일단 문제에서 주어진거처럼

 

뒤에서 1~ 문자열 길이 만큼 잘라서

앞으로 붙여서 B 보다 작다면 ok

 

라는 방식으로 직관적으로 구현햇다

 

대략 길이가 7자리수니까 

주어진 범위가 이백만 정도니까 이백만 x 7 해도 문제가 없음을 알 수 있다.

 

 

#전체 코드

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);


    string sA, sB;
    cin >> sA >> sB;
    int A = stoi(sA), B = stoi(sB);

    int length = (int)sA.size();

    long long ans = 0; 

    for (int x = A; x < B; x++) {
        string sx = to_string(x);

        unordered_set<int> used;


        for (int cutPos = 1; cutPos < length; cutPos++) {

            string backPart = sx.substr(length - cutPos);
            string frontPart = sx.substr(0, length - cutPos);
            string rotated = backPart + frontPart;

     
            if (rotated[0] == '0') continue;

            // 회전 결과 정수
            int y = stoi(rotated);

            if (x < y && y <= B) {
                used.insert(y);
            }
        }

     
        ans += used.size();
    }

    cout << ans << "\n";
    return 0;
}
반응형

+ Recent posts