去哪儿网2018春招软件工程师题解
GitHib:https://github.com/FlushHip/AlgorithmnCode/tree/master/%E5%8E%BB%E5%93%AA%E5%84%BF%E7%BD%912018%E6%98%A5%E6%8B%9B%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88%E7%BC%96%E7%A8%8B%E9%A2%98
全部AC,
1.
BFS
#include <bits/stdc++.h> using namespace std; int main() { string src, des; cin >> src; des = src; reverse(des.begin(), des.end()); vector<string> arr; for (string str; cin >> str; arr.push_back(str)) {} arr.push_back(des); queue<pair<string, int> > que; que.emplace(src, 1); bool isOk = false; while (!que.empty()) { auto now = que.front(); que.pop(); if (now.first == des) { cout << now.second << endl; isOk = true; break; } if (now.second > 99) continue; for (auto it = arr.begin(); it != arr.end(); ++it) { int sum = 0; for (int i = 0; i < (int)now.first.size(); i++) sum += now.first[i] != (*it)[i]; if (sum == 1) que.emplace(*it, now.second + 1); } } if (!isOk) puts("0"); return 0; }
2.
二进制枚举集合
import java.util.*; public class Main { public Scanner cin = new Scanner(System.in); Main() { while (cin.hasNext()) { int n = cin.nextInt(), m = cin.nextInt(); boolean isOk = false; ArrayList<Integer> arr = new ArrayList<Integer>(); for (int i = 0, x; i < n; i++) if ((x = cin.nextInt()) <= m) arr.add(x); for (int i = 0; i < (1 << arr.size()); i++) { int sum = 0; for (int bit = 0; bit < arr.size(); bit++) if ((i & (1 << bit)) != 0) sum += arr.get(bit); if (sum == m) { isOk = true; break; } } System.out.println(isOk ? "perfect" : "good"); } } public static void main(String[] args) { new Main(); } }#春招##去哪儿#