收获:
1. 应该从审题的时候就要留意特殊情况的判断,并且最好要将它们列出来,不然后面的代码实现之后很容易忘掉。
2. 有时候序号从1开始的要求转换成从0开始会更好操作。例如 1 2 3 | 4 5 6, 这两组,问3在哪一组,1/3 = 2/3 != 3/3,可见同一组的数同时用除法向下取整的函数无法得到同一个值,将导致错误。
#include<bits/stdc++.h> #define _for(i,a,b) for(int i=a;i<b;i++) using namespace std; const int maxk = 7777 + 3; int T, K; set<char>s[5]; int a[5]; void solve() { K--; int b[5]; b[4] = 1; for(int i = 3; i >= 0; i--){ b[i] = b[i+1]*s[i+1].size(); //cout << b[i] << endl; } vector<int>idx; for(int i = 0; i < 5; i++){ if(K/b[i] >= s[i].size()){ cout << "NO" <<endl; return ; } idx.push_back(K/b[i]); K %= b[i]; } string ans; _for(i,0,5){ set<char>::iterator it = s[i].begin(); while(idx[i]--) it++; ans.push_back(*it); } cout << ans << endl; } int main() { // freopen("in.txt", "r",stdin); // freopen("out.txt", "w",stdout); cin >> T; while(T--){ set<char>s1[5]; set<char>s2[5]; cin >> K; _for(i,0,6){ string str; cin >> str; _for(j,0,5) s1[j].insert(str[j]); } _for(i,0,6){ string str; cin >> str; _for(j,0,5) s2[j].insert(str[j]); } bool ok = true; _for(i,0,5){ s[i].clear(); set_intersection(s1[i].begin(), s1[i].end(), s2[i].begin(), s2[i].end(), inserter(s[i], s[i].begin())); if(s[i].size() == 0){ ok = false; break; } // for(char c : s[i]) // cout << c; // cout << endl; } if(ok) solve(); else cout << "NO" << endl; } return 0; }
UVA - 1262 Password (注意空集的特殊情况)
原文:https://www.cnblogs.com/sanshi-2018/p/10500802.html