FB - BATLAP

12/05/2023

https://www.facebook.com/photo/?fbid=608562787877923&set=gm.3549556685322147&idorvanity=3482261142051702

Khong biet minh code dung hay sai nua ;-;

Code
#include <iostream>
#include <string>
#define hieuhfgr ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define gcd __gcd
#define pb push_back
#define fi first
#define se second
#define endl "\n"
#define inputfile "BATLAP.INP"
#define outputfile "BATLAP.OUT"
#define file freopen(inputfile, "r", stdin);freopen(outputfile, "w", stdout)
using namespace std;

bool has[10];
int getpos(string s) {
	int digit;
	for (int i=0;i < s.length();i++) {
		digit = s[i] - '0';
		if(has[digit] || digit == 0) return i;
		has[digit]=true;
	}
	return -1;
}

void solve(ll n) {
	fill(has, has + 10, false);
	string s = to_string(n+1);
	int pos = getpos(s);
	if (pos == -1) {
		cout << s;
		return;
	}
	
	//first pos ko thoa man dieu kien
	bool ok = false;
	for (int digit = (s[pos] - '0') + 1; digit <= 9;digit++) {
		if (has[digit]) continue;
		s[pos] = (digit + '0');
		has[digit]=true;
		ok=true;
		break;
	}
	
	if (ok == false) {
		//Truong hop neu nhu ko co so nao phu hop de thay first pos
		for (int i=pos-1;i >= 0;i--) {
			while (has[s[i] - '0'] && (s[i] - '0') <= 9) s[i]++;
			if (s[i] > '9') {
				s[i] = '1';
			}
			else break;
		}
	}
	else {
		//Neu nhu thay duoc
		for(int i=pos+1;i < s.length();i++) {
			for (int digit = 1; digit <= 9; digit++) {
				if (has[digit]) continue;
				s[i] = (digit + '0');
				has[digit]=true;
				break;
			}
		}
	}
	string res=s;
	if (stoll(s) < n+1) {
		//Truong hop 1
		res="";
		for (int i=1;i <= s.length()+1;i++) {
			res += to_string(i);
		}
	}
	cout << res;
	return;
}

int main(){
	hieuhfgr;
	file;
	ll n;
	cin >> n;
	solve(n);
	
	return 0;
}