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

 

생각 흐름

  • 보자마자 슬라이딩 윈도우가 떠올랐다.
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>

using namespace std;

string a,b;

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

    cin>>a>>b;
    
    string tmp = b;
    if(a.size()<b.size()){
        b=a;
        a=tmp;
    }
    
    int idx = (int)b.size()-1;
    
    string now = "";
    now+=b[idx--];
    
    int answer = 987654321;
    
    while(now.size()!=a.size()+b.size()){
        bool ok = true;
        for(int i = 0; i<min(a.size(), now.size()); i++){
            if(a[i]==now[i] && now[i]=='2'){
                ok=false;
                break;
            }
        }
        if(ok){
            int m;
            if(idx<0){
                m=(int)max(a.size(), now.size());
            }else{
                m=(int)a.size()+idx+1;
            }
            answer = min(answer, m);
        }
        if(idx>-1){
            now=b[idx--]+now;
        }else{
            now='1'+now;
        }
    }
    
    if(answer==987654321){
        answer=a.size()+b.size();
    }
    
    cout<<answer<<endl;
    
    return 0;
}

+ Recent posts