noi题库答案 c语言,NOIP2004提高组复赛试题答案c语言版

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

bool finish, hash[256], used[27];

int n, stk[27];

string a, b, c;

string word;

void init() {

fin >> n >> a >> b >> c;

finish = false;

}

void outsol() {

int i, ans[27];

for (i = 0; i 

ans[word[i] - 65] = stk[i];

fout <

for (i = 1; i 

fout <

fout <

finish = true;

}

void addup(char ch) {

if (!hash[ch]) {

hash[ch] = true;

word = word + ch;

}

}

string change(string str, char x, char y) {

for (int i = 0; i 

if (str[i] == x)

str[i] = y;

return str;

}

void pre_doing() {

word = "";

memset(hash, 0, sizeof(hash));

for (int i = n - 1; i >= 0; i --) {

addup(a[i]); addup(b[i]); addup(c[i]);

}

memset(used, 0, sizeof(used));

}

bool bad() {

int p, g = 0;

for (int i = n - 1; i >= 0; i --) {

if (a[i] >= n || b[i] >= n || c[i] >= n) return false;

p = a[i] + b[i] + g;

if (p % n != c[i]) return true;

g = p / n;

p %= n;

}

return false;

}

bool modcheck() {

int i, p, p1, p2, g = 0;

//a + b = c, all know

for (i = n - 1; i >= 0; i --) {

if (a[i] >= n || b[i] >= n || c[i] >= n) continue;

p = (a[i] + b[i]) % n;

if (!(p == c[i] || (p + 1) % n == c[i])) return true;

}

//a + ? = c

for (i = n - 1; i >= 0; i --) {

if (!(a[i] = n)) continue;

p1 = (c[i] - a[i] + n) % n;

p2 = (p1 - 1) % n;

if (used[p1] && used[p2]) return true;

}

//? + b = c

for (i = n - 1; i >= 0; i --) {

if (!(a[i] >= n && c[i] 

p1 = (c[i] - b[i] + n) % n;

p2 = (p1 - 1) % n;

if (used[p1] && used[p2]) return true;

}

//a + b = ?

for (i = n - 1; i >= 0; i --) {

if (!(a[i] = n)) continue;

p1 = (a[i] + b[i]) % n;

p2 = (p1 + 1) % n;

if (used[p1] && used[p2]) return true;

}

return false;

}

void dfs(int l) {

int i;

string A, B, C;

if (finish) return;

if (bad()) return;

if (modcheck()) return;

if (l == n) {

outsol();

return;

}

for (i = n - 1; i >= 0; i --)

if (!used[i]) {

used[i] = true; A = a; B = b; C = c;

a = change(A, word[l], i);

b = change(B, word[l], i);

c = change(C, word[l], i);

stk[l] = i;

dfs(l + 1);

used[i] = false; a = A; b = B; c = C;

}

}

int main() {

init();

pre_doing();

dfs(0);

return 0;

}

搜索题的框架往往不难找到,关键就是在搜索的优化上,本文的主要篇幅也就是讨论了几种有效的优化。搜索问题的优化更多的需要选手的经验和思考、分析问题的能力,所以搜索剪枝也是竞赛中经久不衰的经典问题�

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值