結果

問題 No.2069 み世界数式
ユーザー ripity
提出日時 2023-06-22 16:44:15
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 108 ms / 2,000 ms
コード長 2,086 bytes
コンパイル時間 2,292 ms
コンパイル使用メモリ 204,904 KB
最終ジャッジ日時 2025-02-15 00:08:41
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int M;

vector<string> expression(string &s, int &i);
vector<string> term(string &s, int &i);
vector<string> factor(string &s, int &i);
vector<string> number(string &s, int &i);

vector<string> expression(string &s, int &i) {
	vector<string> dp = term(s, i), ep(M+1, "X"), fp;
	while( s[i] == '$' ) {
		i++;
		fill(ep.begin(), ep.end(), "X");
		fp = term(s, i);
		for( int x = 0; x <= M; x++ ) {
			for( int y = 0; y <= M-x; y++ ) {
				if( ep[x+y] == "X" && dp[x] != "X" && fp[y] != "X" ) {
					ep[x+y] = dp[x]+"+"+fp[y];
				}
			}
		}
		for( int x = 0; x <= M; x++ ) {
			for( int y = 0; y <= x; y++ ) {
				if( ep[x-y] == "X" && dp[x] != "X" && fp[y] != "X" ) {
					ep[x-y] = dp[x]+"-"+fp[y];
				}
			}
		}
		swap(dp, ep);
	}
	return dp;
}
vector<string> term(string &s, int &i) {
	vector<string> dp = factor(s, i), ep(M+1, "X"), fp;
	while( s[i] == '&' ) {
		i++;
		fill(ep.begin(), ep.end(), "X");
		fp = factor(s, i);
		for( int x = 0; x <= M; x++ ) {
			for( int y = 0; x*y <= M; y++ ) {
				if( ep[x*y] == "X" && dp[x] != "X" && fp[y] != "X" ) {
					ep[x*y] = dp[x]+"*"+fp[y];
				}
				if( x == 0 ) break;
			}
		}
		for( int x = 0; x <= M; x++ ) {
			for( int y = 1; y <= M; y++ ) {
				if( ep[x/y] == "X" && dp[x] != "X" && fp[y] != "X" ) {
					ep[x/y] = dp[x]+"/"+fp[y];
				}
			}
		}
		swap(dp, ep);
	}
	return dp;
}
vector<string> factor(string &s, int &i) {
	vector<string> dp;
	if( s[i] == '(' ) {
		i++;
		dp = expression(s, i);
		i++;
	}else {
		dp = number(s, i);
	}
	return dp;
}
vector<string> number(string& s, int &i) {
	int ret = 0;
	vector<string> dp(M+1, "X");
	while( isdigit(s[i]) ) {
		ret = 10*ret+(s[i]-'0');
		i++;
	}
	dp[ret] = "";
	return dp;
}

int main() {
	int ans;
	string expr;
	cin >> M >> ans >> expr;
	int _i = 0;
	vector<string> decode = expression(expr, _i);
	if( decode[ans] == "X" ) {
		cout << -1 << endl;
	}else {
		for( int i = 0, j = 0; i < expr.size(); i++ ) {
			if( expr[i] == '$' || expr[i] == '&' ) {
				expr[i] = decode[ans][j];
				j++;
			}
		}
		cout << expr << endl;
	}
}
0