1019. General Palindromic Number
题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示。
思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回文。
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int Base[30]; 5 int main() 6 { 7 int n, b; 8 scanf("%d%d", &n, &b); 9 int len = 0, tmp = n,yu=tmp%b; 10 while (tmp / b) 11 { 12 Base[len++] = yu; 13 tmp = tmp / b; 14 yu = tmp % b; 15 } 16 Base[len++] = yu; 17 bool flag = true; 18 for (int i = 0, j = len - 1; i <= j&&flag; i++, j--) 19 { 20 if (Base[i] != Base[j]) flag = false; 21 } 22 if (flag) printf("Yes "); 23 else printf("No "); 24 for (int i = len - 1; i >= 0; i--) 25 { 26 printf("%d", Base[i]); 27 if (i) printf(" "); 28 } 29 printf(" "); 30 return 0; 31 }
1020. Tree Traversals
题意:给出二叉树的后序遍历和中序遍历,求层次遍历。
思路:通过后序遍历和中序遍历建立二叉树,然后对二叉树进行BFS。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 const int maxn = 40; 6 int postOrder[maxn]; 7 int inOrder[maxn]; 8 int L[maxn],R[maxn]; 9 int treeRoot; 10 queue<int>q; 11 int createBinTree(int pt_pl,int pt_pr,int pt_i,int n) 12 { 13 if (n == 0) return -1; 14 int k = pt_i,curRoot= postOrder[pt_pr]; 15 while (curRoot!= inOrder[k])k++;//在中序中找到当前子树的根 16 L[curRoot] = createBinTree(pt_pl,pt_pl+(k- pt_i)-1,pt_i, k-pt_i); 17 R[curRoot] = createBinTree(pt_pl + (k - pt_i),pt_pr-1,k+1,n - (k-pt_i) - 1); 18 return curRoot; 19 } 20 void printLevelOrder(int root) 21 { 22 q.push(root); 23 while (!q.empty()) 24 { 25 int u = q.front(); 26 q.pop(); 27 if (u == treeRoot) printf("%d", u); 28 else printf(" %d", u); 29 if (L[u] != -1) q.push(L[u]); 30 if (R[u] != -1) q.push(R[u]); 31 } 32 printf(" "); 33 } 34 35 int main() 36 { 37 int n; 38 scanf("%d", &n); 39 for (int i = 0; i < n; i++) scanf("%d", postOrder + i); 40 for (int i = 0; i < n; i++) scanf("%d", inOrder + i); 41 treeRoot = postOrder[n-1]; 42 createBinTree(0,n - 1, 0, n); 43 printLevelOrder(treeRoot); 44 return 0; 45 }
1021. Deepest Root
题意:给出n个点和n-1条边。若是只有一个连通分量,且为无环图,则可将其看成一棵树,求拥有最大深度的树的所有根,并按字典序升序输出。否则输出连通块的数目。
思路:并查集求连通块;DFS判断是否有环(傻了,如果只有1个连通块,则因为边数只有n-1条,只能是无环图);BFS求树的深度。可通过连续两次BFS求出树的最大直径。
1 #include<iostream> 2 #include<set> 3 #include<queue> 4 #include<cstring> 5 #include<cstdio> 6 using namespace std; 7 const int maxn = 10010; 8 int pre[maxn],cnt_cpt; 9 set<int>ans; 10 int lvl[maxn]; 11 bool vis[maxn]; 12 struct edge 13 { 14 int to,next; 15 edge(int tt=0,int nn=0):to(tt),next(nn){} 16 }; 17 edge Edge[maxn << 1]; 18 int Head[maxn], totedge; 19 void addedge(int from, int to) 20 { 21 Edge[totedge] = edge(to, Head[from]); 22 Head[from] = totedge++; 23 Edge[totedge] = edge(from, Head[to]); 24 Head[to] = totedge++; 25 26 } 27 int max_deep; 28 int Find(int x) 29 { 30 if (pre[x] == x) return x; 31 else 32 { 33 int fa = pre[x]; 34 pre[x] = Find(fa); 35 return pre[x]; 36 } 37 } 38 void Join(int u, int v) 39 { 40 int fu = Find(u), fv = Find(v); 41 if (fu != fv) 42 { 43 if (fv > fu) fv ^= fu, fu ^= fv, fv ^= fu; 44 pre[fu] = fv; 45 cnt_cpt--; 46 } 47 } 48 void BFS(int u) 49 { 50 vis[u] = true; 51 queue<int>q; 52 q.push(u); 53 while (!q.empty()) 54 { 55 int v = q.front(); 56 q.pop(); 57 for (int i = Head[v]; i != -1; i = Edge[i].next) 58 { 59 int t = Edge[i].to; 60 if (!vis[t]) 61 { 62 vis[t] = true; 63 lvl[t] = lvl[v] + 1; 64 if (lvl[t] > max_deep) max_deep = lvl[t]; 65 q.push(t); 66 } 67 } 68 } 69 } 70 //bool DFS(int u,int pre) 71 //{ 72 // vis[u] = true; 73 // for (int i = Head[u]; i != -1; i = Edge[i].next) 74 // { 75 // int v = Edge[i].to; 76 // if (v == pre) continue; 77 // if (vis[v]) return true; 78 // else 79 // { 80 // bool flag = DFS(v, u); 81 // if (flag) return true; 82 // } 83 // } 84 // vis[u] = false; 85 // return false; 86 //} 87 int main() 88 { 89 int n; 90 scanf("%d", &n); 91 92 for (int i = 1; i <= n; i++) pre[i] = i; 93 cnt_cpt = n; 94 totedge = 0; 95 memset(Head, -1, sizeof(Head)); 96 97 for (int i = 1; i < n; i++) 98 { 99 int u, v; 100 scanf("%d%d", &u, &v); 101 Join(u, v); 102 addedge(u, v); 103 } 104 if (cnt_cpt > 1) 105 { 106 printf("Error: %d components ", cnt_cpt); 107 } 108 else 109 { 110 max_deep = 0; 111 BFS(1); 112 int next_root; 113 for (int i = 1; i <= n; i++)if (lvl[i] == max_deep) ans.insert(i), next_root=i; 114 max_deep = 0; 115 memset(lvl, 0, sizeof(lvl)); 116 memset(vis, 0, sizeof(vis)); 117 BFS(next_root); 118 for(int i=1;i<=n;i++)if (lvl[i] == max_deep) ans.insert(i); 119 int sz = ans.size(); 120 set<int>::iterator it = ans.begin(); 121 for (; it != ans.end(); it++) 122 { 123 printf("%d ", *it); 124 } 125 } 126 return 0; 127 }
1022. Digital Library
题意:给出n个图书信息:ID、title、author、key words、publisher、publish year.之后有m个询问,求指定title/author/key word/publisher/publish year的图书的ID。
思路:用map建立title、author、key word、publisher、publish year到ID的映射,用set存放被映射的ID。
一些技巧:sstream的使用、用fgets(str,size,stdin)或scanf("%[^ ]")读入一行带有空格的字符串(注意用getchar()吸收‘ ’).
1 #include<iostream> 2 #include<map> 3 #include<set> 4 #include<string> 5 #include<sstream> 6 #include<cstdio> 7 using namespace std; 8 9 map<string, set<string> >mp[5]; 10 char tmp[90]; 11 string id,t1,t2; 12 int main() 13 { 14 int n; 15 scanf("%d", &n); 16 for (int i = 0; i < n; i++) 17 { 18 scanf("%s", tmp);//ID,fgets(tmp,90,stdin); 19 id = tmp; 20 getchar(); 21 scanf("%[^ ]", tmp);//title 22 mp[0][tmp].insert(id); 23 getchar(); 24 scanf("%[^ ]", tmp);//author 25 mp[1][tmp].insert(id); 26 getchar(); 27 scanf("%[^ ]", tmp);//key words 28 stringstream sss((t1=tmp)); 29 while (sss >> t2) mp[2][t2].insert(id); 30 getchar(); 31 scanf("%[^ ]", tmp);//publisher 32 mp[3][tmp].insert(id); 33 getchar(); 34 scanf("%[^ ]", tmp);//year 35 mp[4][tmp].insert(id); 36 } 37 int m; 38 scanf("%d", &m); 39 for (int i = 1; i <= m; i++) 40 { 41 getchar(); 42 scanf("%[^ ]", tmp); 43 printf("%s ", tmp); 44 set<string>&st = mp[tmp[0] - '1'][tmp + 3]; 45 if (st.size() == 0) printf("Not Found "); 46 else 47 { 48 set<string>::iterator it = st.begin(); 49 for (; it != st.end(); it++) 50 { 51 printf("%s ", (*it).c_str()); 52 } 53 } 54 } 55 return 0; 56 }
1023. Have Fun with Numbers
题意:将给出的一个大数*2,判断其是否可由原来的数通过排列得到。
思路:数组模拟大数*2.
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 char str[30],dst[30]; 6 int main() 7 { 8 scanf("%s", str + 1); 9 int len = strlen(str + 1); 10 int tadd = 0; 11 for (int i = len; i >= 1; i--) 12 { 13 tadd += 2*(str[i] - '0'); 14 dst[i] = '0'+tadd % 10; 15 tadd /= 10; 16 } 17 if (tadd) dst[0] = '0'+tadd; 18 dst[len + 1] = '