PAT (Advanced Level) Practice 散列題ios
將元素經過一個函數轉換爲整數,使得該整數能夠儘可能惟一地表明這個元素。算法
通常直接用STL中的 map 或 unordered_map,除非必須模擬這些方法或對算法效率要求較高,不然不須要本身實現解決衝突的方法數組
取 key 平方的中間若干位做爲 hash 值函數
H(key) = key % mod (表長Tsize >= mod,不然會越界)spa
將一個字符串S映射爲一個整數,使得整數能夠儘量惟一地表明字符串S.net
將大寫字母 A-Z 視爲 0-25,即對應到了26進制中,再轉換爲10進制。轉換爲的整數最大爲26^len-1,len爲字符串長度code
int hashFunc(char S[], int len){ int id = 0; for (int i = 0; i < len; i++){ id = id * 26 + (S[i] - 'A'); } return id; }
52進制轉10進制問題排序
int hashFunc(char S[], int len){ int id = 0; for (int i = 0; i < len - 1; i++){ id = id * 26 + (S[i] - 'A'); } id = id * 10 + (S[len-1] - '0'); return id; }
#include<iostream> using namespace std; int main() { int k, n, sum = 0; double a, p[1001] = {0}; scanf("%d", &k); for (int i = 0; i < k; i++){ scanf("%d%lf", &n, &a); p[n] += a; } scanf("%d", &k); for (int i = 0; i < k; i++){ scanf("%d%lf", &n, &a); p[n] += a; } for (int i = 0; i < 1001; i++) if (p[i]) sum++; printf("%d", sum); for (int i = 1000; i >= 0; i--) if (p[i]) printf(" %d %.1f", i, p[i]); return 0; }
#include<iostream> #include<map> using namespace std; int main() { int k, n, sum = 0; double a, p1[1002] = {0}, p2[1002] = {0}, p[1002] = {0}; scanf("%d", &k); for (int i = 0; i < k; i++){ scanf("%d%lf", &n, &a); p1[n] = a; } scanf("%d", &k); for (int i = 0; i < k; i++){ scanf("%d%lf", &n, &a); p2[n] = a; } for (int i = 0; i < 1002; i++) p[i] = p1[i] + p2[i]; map<int,double,greater<int>> p_map; for (int i = 0; i < 1002; i++){ if (p[i]){ sum++; p_map[i] = p[i]; } } printf("%d", sum); for (auto it : p_map) printf(" %d %.1f", it.first, it.second); return 0; }
#include<cstdio> double result[2001] = {0}; int main() { int k1, k2, exp2; double coe2; scanf("%d", &k1); int *exp1 = new int[k1]; double *coe1 = new double[k1]; for (int i = 0; i < k1; i++) scanf("%d%lf", exp1+i, coe1+i); scanf("%d", &k2); for (int i = 0; i < k2; i++){ scanf("%d%lf", &exp2, &coe2); for (int j = 0; j < k1; j++) result[exp1[j] + exp2] += coe1[j] * coe2; } int k = 0; for (int i = 0; i < 2001; i++) if (result[i] != 0) k++; printf("%d",k); for (int i = 2000; i >= 0; i--) if (result[i] != 0) printf(" %d %.1f", i, result[i]); }
#include<iostream> #include<set> using namespace std; int main() { string a, b; cin >> a >> b; set<char> printed; for (int i = 0; i < a.length(); i++){ if (b.find(a[i]) == b.npos && printed.find(toupper(a[i])) == printed.end()){ printf("%c", toupper(a[i])); printed.insert(toupper(a[i])); } } return 0; }
#include<iostream> using namespace std; int main() { string a, b, broken; cin >> a >> b; for (int i = 0; i < a.length(); i++) if (b.find(a[i]) == b.npos && broken.find(toupper(a[i])) == broken.npos) broken += toupper(a[i]); cout << broken; return 0; }
#include<iostream> #include<set> using namespace std; int main() { string a, b; cin >> a >> b; int p = 0; set<char> broken, okay, printed; for (int i = 0; i < a.length(); i++){ if (a[i] == b[p]){ p++; okay.insert(toupper(a[i])); if (broken.find(toupper(a[i])) != broken.end()) broken.erase(toupper(a[i])); } else if (okay.find(toupper(a[i])) == okay.end()) broken.insert(toupper(a[i])); } for (int i = 0; i < a.length(); i++){ if (broken.find(toupper(a[i])) != broken.end() && printed.find(toupper(a[i])) == printed.end()){ printed.insert(toupper(a[i])); printf("%c", toupper(a[i])); } } return 0; }
#include<iostream> #include<map> using namespace std; int main() { map<char,int> shop; string s, e; cin >> s >> e; for (int i = 0; i < s.length(); i++) shop[s[i]]++; int miss = 0; for (int i = 0; i < e.length(); i++){ if (shop.find(e[i]) != shop.end() && shop[e[i]] > 0) shop[e[i]]--; else miss++; } if (!miss) printf("Yes %d", s.length()-e.length()); else printf("No %d", miss); return 0; }
#include<iostream> #include<map> using namespace std; int main() { map<char,int> shop; string s, e; cin >> s >> e; for (int i = 0; i < s.length(); i++) shop[s[i]]++; int extra = 0, miss = 0; for (int i = 0; i < e.length(); i++){ if (shop.find(e[i]) != shop.end() && shop[e[i]] > 0) shop[e[i]]--; else miss++; } if (!miss){ for (auto it = shop.begin(); it != shop.end(); it++) extra += it->second; printf("Yes %d", extra); } else printf("No %d", miss); return 0; }
#include<unordered_map> #include<iostream> #include<set> using namespace std; bool isPrime(int n){ if (n <= 1) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } int main() { int n, k, id; scanf("%d", &n); unordered_map<int,int> rank; for (int i = 0; i < n; i++){ scanf("%d", &id); rank.insert({id,i+1}); } set<int> checked; scanf("%d", &k); for (int i = 0; i < k; i++){ scanf("%d", &id); if (rank.find(id) == rank.end()) printf("%04d: Are you kidding?\n", id); else { if (checked.find(id) != checked.end()) printf("%04d: Checked\n", id); else { checked.insert(id); if (rank[id] == 1) printf("%04d: Mystery Award\n", id); else if (isPrime(rank[id])) printf("%04d: Minion\n", id); else printf("%04d: Chocolate\n", id); } } } return 0; }
map.insert({key,value})
map 插入值可直接用 {} 括起來,省去寫 pair 的麻煩#include<iostream> #include<algorithm> #include<vector> #include<set> using namespace std; int couple[100000]; int main() { int n, m, a, b; scanf("%d", &n); fill(couple,couple+100000,-1); for (int i = 0; i < n; i++){ scanf("%d%d", &a, &b); couple[a] = b; couple[b] = a; } scanf("%d", &m); set<int> guests; for (int i = 0; i < m; i++){ scanf("%d", &a); guests.insert(a); } vector<int> lonely; for (auto it = guests.begin(); it != guests.end(); it++) if (couple[*it] < 0 || guests.find(couple[*it]) == guests.end()) lonely.push_back(*it); if (lonely.empty()) printf("0\n"); else{ sort(lonely.begin(),lonely.end()); printf("%d\n%05d",lonely.size(), lonely[0]); for (int i = 1; i < lonely.size(); i++) printf(" %05d",lonely[i]); } return 0; }
memset(數組名, 值, sizeof(數組名);
在<cstring>中,建議只用來賦 0 或 -1fill(first, last, value );
在<algorithm>中,可將 first-last 範圍內填充爲 value 值set.find (value);
在集合中查找 value 值,找到返回 iterator,不然返回 set::end
.