洛谷_P6387 [COCI2007-2008#4] VECI(尚贤)

题目传送门

DFS! DFS! DFS!

用dfs枚举第一位,第二位,……,第str.size()位;
然后判断……
详见AC代码

#include <iostream>
#include <cstdio>
#include <string>
#define SIZE 10
using namespace std;
string str, ans, temp;
bool vis[SIZE];

void dfs(const int &);
bool pan_duan(const string &, const string &, const string &);
bool compare(const string &, const string &, const string &);

int main() {
	freopen("cpp.in", "r", stdin);
	freopen("cpp.out", "w", stdout);
	cin >> str;
	dfs(1);
	cout << ((ans == "") ? "0" : ans) << endl;
	return 0;
}

void dfs(const int &t) {
	for (int i = 0; i < str.size(); ++i) {
		if (!vis[i]) {
			vis[i] = true;
			temp += str[i];
			if (t == str.size()) {
				if (pan_duan(str, temp, ans)) {
					ans = temp;
				}
			} else {
				dfs(t + 1);
			}
			temp.erase(temp.size() - 1);
			vis[i] = false;
		}
	}
}
bool pan_duan(const string &x, const string &y, const string &z) {
	if (compare(str, ">", temp) || compare(str, "==", temp)) {
		return false;
	}
	if (z == "" || (compare(y, "<", z))) {
		return true;
	}
	return false;
}
bool compare(const string &x, const string &ch, const string &y) {
	if (ch == "==") {
		return (x == y);
	}
	if (ch == "<") {
		if (x.size() < y.size()) {
			return true;
		}
		if (x.size() > y.size()) {
			return false;
		}
		return (x < y);
	}
	if (ch == ">") {
		if (x.size() > y.size()) {
			return true;
		}
		if (x.size() < y.size()) {
			return false;
		}
		return x > y;
	}
}
发布了33 篇原创文章 · 获赞 0 · 访问量 167

猜你喜欢

转载自blog.csdn.net/weixin_42790071/article/details/105563639