标签:double ack 个数 for pre 长度 limit color The
3 22 10 3 7 0 1 2 10 1 1 25 16 3 A B C
110 give me the bomb please CCB
kuangbin搜索好像有个类似的题目,也是找只能由几个数构成某个数的倍数,不同的是那个题只能有01构成而且有SPJ,这道题是输入且输出字典序最小的。
做题历程=7=,最开始思路错了去从n的倍数开始搜TLE,然后正确的思路没有判断取模时0的情况RE,再然后没有把char换成intWA,=7=简直了
思路就是从给定的几个数开始搜索,然后每次加上那几个数,取模判重一下就行了。
值得注意的是因为涉及到进制,一定要注意字母和数字之间的转换。
代码:
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <sstream> 6 #include <iomanip> 7 #include <map> 8 #include <stack> 9 #include <deque> 10 #include <queue> 11 #include <vector> 12 #include <set> 13 #include <list> 14 #include <cstring> 15 #include <cctype> 16 #include <algorithm> 17 #include <iterator> 18 #include <cmath> 19 #include <bitset> 20 #include <ctime> 21 #include <fstream> 22 #include <limits.h> 23 #include <numeric> 24 25 using namespace std; 26 27 #define F first 28 #define S second 29 #define mian main 30 #define ture true 31 32 #define MAXN 1000000+5 33 #define MOD 1000000007 34 #define PI (acos(-1.0)) 35 #define EPS 1e-6 36 #define MMT(s) memset(s, 0, sizeof s) 37 typedef unsigned long long ull; 38 typedef long long ll; 39 typedef double db; 40 typedef long double ldb; 41 typedef stringstream sstm; 42 const int INF = 0x3f3f3f3f; 43 44 map<int,int>mp,vis; 45 int n,k,m; 46 47 int Mod(string x){ 48 int res = 0; 49 for(int i = 0; i < x.size(); i++){ 50 res *= k; 51 if(isdigit(x[i])){ //WA那次就是这里忘记转换数字了=7= 52 res += x[i] - ‘0‘; 53 } 54 else{ 55 res += x[i] - ‘A‘ + 10; 56 } 57 res %= n; 58 //cout << res << " "; 59 } 60 return res; 61 } 62 63 int bfs(){ 64 queue<string>q; 65 for(int i = 1; i <= 16; i++){ 66 if(mp[i]){ 67 string a = ""; 68 a = a + char(i>=10?char(‘A‘+i-10):char(‘0‘+i)); //注意转换 69 int flag = Mod(a); 70 if(flag == 0){ 71 cout << a << endl; 72 return 0; 73 } 74 else{ 75 if(vis[flag] == 0){ 76 vis[flag]++; 77 q.push(a); 78 //cout << a << " " << a.size() << " " << flag << endl; 79 } 80 } 81 } 82 } 83 while(!q.empty()){ 84 string tp = q.front(); 85 q.pop(); 86 if(tp.size() >= 500){ 87 return -1; 88 } 89 for(int i = 0; i <= 16; i++){ 90 if(mp[i]){ 91 string nxt = tp; 92 nxt = nxt + char(i>=10?char(‘A‘+i-10):char(‘0‘+i)); 93 int flag = Mod(nxt); 94 if(flag == 0){ 95 cout << nxt << endl; 96 return 0; 97 } 98 else{ 99 if(vis[flag] == 0){ 100 vis[flag]++; 101 q.push(nxt); 102 //cout << nxt << " " << nxt.size() << " " << flag <<endl; 103 } 104 } 105 } 106 } 107 } 108 return -1; 109 110 } 111 112 int main(){ 113 ios_base::sync_with_stdio(false); 114 cout.tie(0); 115 cin.tie(0); 116 int t; 117 cin>>t; 118 while(t--){ 119 mp.clear(); 120 vis.clear(); 121 cin>>n>>k>>m; 122 for(int i = 0; i < m; i++){ 123 char tp; 124 cin>>tp; 125 if(isdigit(tp)){ 126 mp[tp - ‘0‘]++; 127 } 128 else{ 129 mp[tp - ‘A‘ + 10]++; 130 } 131 } 132 if(n == 0){ //特判n为0的情况 133 if(mp[0]){ 134 cout << 0 << endl; 135 continue; 136 } 137 else{ 138 cout << "give me the bomb please" << endl; 139 continue; 140 } 141 } 142 if(bfs() == -1){ 143 cout << "give me the bomb please" << endl; 144 } 145 } 146 return 0; 147 }
标签:double ack 个数 for pre 长度 limit color The
原文地址:https://www.cnblogs.com/xenny/p/9384896.html