/* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> #include <map> #include <set> #include <cmath> #include <string> #include <cstring> using namespace std; int main(void) { //freopen ("A.in", "r", stdin); map<char, int> m1; map<char, int> m2; int n; while (~scanf ("%d", &n)) { for (int i=1; i<=26; ++i) { m1[i] = 0; m2[i] = 0; } char s[110]; scanf ("%s", &s); for (int i=0; i<=n-1; ++i) { if (s[i]<='z' && s[i]>='a') { int x = s[i] - 'a' + 1; m1[x]++; } else { int y = s[i] - 'A' + 1; m2[y]++; } } bool flag = true; for (int i=1; i<=26; ++i) { if (m1[i] == 0 && m2[i] == 0) { flag = false; break; } } (flag) ? puts ("YES") : puts ("NO"); } return 0; }
BFS B. Two Buttons
题意:给出n,m两个数字,n可以*2,或者-1,问最少几步n变成m
思路:BFS:从n出发,分两条路(a, b),标记计算后的数字,如果没找到,入队;如果找到了则输出,不入队,BFS结束。详细解释
#include <cstdio> #include <iostream> #include <algorithm> #include <map> #include <set> #include <cmath> #include <queue> #include <string> #include <cstring> using namespace std; const int MAXN = 2e4 + 10; const int INF = 0x3f3f3f3f; int dp[MAXN]; int used[MAXN]; struct NODE { int x; int cnt; }s; void BFS(int n, int m) { if (n >= m) { printf ("%d\n", n - m); return ; } queue<struct NODE> q; s.x = n; s.cnt = 0; used[n] = 1; q.push (s); NODE a, b; while (!q.empty()) { a = q.front(); b = q.front(); q.pop(); a.x *= 2; a.cnt++; b.x -= 1; b.cnt++; if (a.x == m) { printf ("%d\n", a.cnt); break; } if (b.x == m) { printf ("%d\n", b.cnt); } if (a.x > 0 && a.x < MAXN && !used[a.x]) { q.push (a); used[a.x] = 1; } if (b.x > 0 && b.x < MAXN && !used[b.x]) { q.push (b); used[b.x] = 1; } } } int main(void) { //freopen ("B.in", "r", stdin); int n, m; int ans; while (~scanf ("%d%d", &n, &m)) { memset (used, 0, sizeof (used)); BFS (n, m); } return 0; }