习题6-1,UVa673,Time:11.1
#include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include <stack> using namespace std; int main() { int Case; string str; bool flag; scanf("%d",&Case); for(int c = 1; c <= Case; c++) { stack<char>q; flag = true; cin>>str; for(int i = 0; i < str.length(); i++) { if(q.size() == 0 || str[i]=='(' || str[i]=='[') q.push(str[i]); else { if((str[i]==')' && q.top()=='(') || (str[i]==']' && q.top()=='[')) { q.pop(); } else { flag = false; break; } } } if(flag==false || q.size()) cout<<"NO"<<endl; else cout<<"YES"<<endl; } return 0; }
习题6-2,UVa712,Time:11.1
#include <iostream> #include <cstdio> #include <string> #include <cstring> using namespace std; struct node { int l, r, s; }num[1<<8+10]; int n, m; int ans[1005]; string str; int build(int i, int l, int r) { num[i].l = l; num[i].r = r; if(l == r) { num[i].s = str[l-1] - '0'; } else { build(i+i, l, (l+r)>>1); build(i+i+1,((l+r)>>1)+1,r); } } int solve() { int k = 1; for(int i = 0; i < n; i++) { k+=k; k+=(str[i] - '0'); } return num[k].s; } int main() { while( cin>>n ) { cin>>str; build(1, 1, 1<<n); cin>>m; for(int i = 0; i < m; i++) { cin>>str; ans[i] = solve(); } for(int i = 0; i < m; i++) cout<<ans[i]; cout<<endl; } return 0; }
习题6-3,UVa536,Time:11.2
#include <iostream> #include <cstdio> #include <string> #include <cstring> using namespace std; char pre_order[100005]; char in_order[100005]; char post_order[100005]; int pl, il; int dfs(char* spo, char* fpo, char* sio, char* fio) { if(fpo < spo || fio < sio) return 0; char g = *spo; int sn = 0; while(*(sio + sn) != g) sn++; dfs(spo + 1, spo + sn, sio, sio + sn); dfs(spo + sn + 1, fpo, sio + sn + 1, fio); cout<<*spo; } int main() { freopen("in.txt","r",stdin); while( cin>>pre_order>>in_order ) { pl = strlen(pre_order); il = strlen(in_order); dfs(pre_order, pre_order + pl - 1, in_order, in_order + il - 1); cout<<endl; } return 0; }
习题6-4,UVa439,Time:11.2
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <queue> using namespace std; int d[10][2] = {{1,2},{2,1},{-1,2},{2,-1},{-2,1},{1,-2},{-1,-2},{-2,-1}}; int map[10][10]; struct node { bool operator <(const node &a)const { return a.s<s; } int x, y, s; }; int main() { int x, y; int ex, ey; node v, u; freopen("in.txt","r",stdin); while(cin>>x>>y) { memset(map,0,sizeof(map)); cin>>ex>>ey; priority_queue<node>q; v.x = x; v.y = y; v.s = 0; q.push(v); while(1) { v = q.top(); q.pop(); if(map[v.x][v.y] == 0) map[v.x][v.y] = v.s; else continue; if(v.x == ex && v.y == ey) { cout<<v.s; break; } for(int i = 0; i < 8; i++) { u.x = v.x + d[i][0]; u.y = v.y + d[i][1]; u.s = v.s + 1; if(u.x > 0 && u.y <= 8 && u.x <=8 && u.y >0 && map[u.x][u.y] == 0) q.push(u); } } } return 0; }
习题6-5,UVa1600,Time:11.2
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <queue> using namespace std; int map[25][25]; int stp[25][25]; int d[5][2]={{1,0},{0,1},{-1,0},{0,-1}}; struct node { bool operator <(const node &a )const { return a.s<s; } int x, y, s, k; }; int main() { freopen("in.txt","r",stdin); int m, n, x; node v, u; while(cin>>m>>n) { memset(map,0,sizeof(map)); memset(stp,0,sizeof(stp)); priority_queue<node>q; for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) cin>>map[i][j]; v.x = 1; v.y = 1; v.s = 1; stp[v.x][v.y] = v.s; q.push(v); while(1) { v = q.top(); q.pop(); if(v.x == m && v.y == n) { cout<<v.s-1<<endl; break; } stp[v.x][v.y] = v.s; for(int i = 0; i < 4; i++) { u.x = v.x + d[i][0]; u.y = v.y + d[i][1]; u.s = v.s + 1; if(u.x > 0 && u.x <=m && u.y > 0 && u.y <= n && stp[u.x][u.y]==0 && v.k*map[u.x][u.y]==0) { u.k = map[u.x][u.y]; q.push(u); } } } } return 0; }
习题6-6,UVa1600,Time:11.2
/* 5 1 2 1 3 2 4 2 5 0 0 6 3 7 */ #include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <map> #include <queue> using namespace std; vector<int>tree[2005]; int num[2005]; int cur[2005]; void sec_dfs(int n, int f) { for(int i = 0; i < tree[n].size(); i++) { if(tree[n][i] != f) { cur[tree[n][i]] = cur[n] >> 1; sec_dfs(tree[n][i],n); } } } int fir_dfs(int n, int f) { int s = 0; int m = 0; for(int i = 0; i < tree[n].size(); i++) { if(tree[n][i] != f) { m = fir_dfs(tree[n][i], n); s = max(m, s); } } if(s == 0) return 1; return 2*s; } int ans(int n) { int maxx = 0; int s = 0; map<double,int>mul; for(int i = 1; i <= n; i++) if(num[i]) { s++; mul[(num[i]*1.0 / cur[i]*1.0)]++; if(mul[(num[i]*1.0 / cur[i]*1.0)] > maxx) maxx = mul[(num[i]*1.0 / cur[i]*1.0)]; } return s-maxx; } int main() { freopen("in.txt","r",stdin); int n, m; int s, t; while(cin>>n) { m = 0; for(int i = 1; i < n; i++) { cin>>s>>t; tree[s].push_back(t); tree[t].push_back(s); } for(int i = 1; i <= n; i++) { cin>>num[i]; m = num[i]>m?num[i]:m; } cur[1] = fir_dfs(1,0); sec_dfs(1,0); // for(int i = 1; i <= n; i++) // cout<<cur[i]<<" "; cout<<ans(n)<<endl; } return 0; }
习题6-8,UVa806,Time:11.4
#include <iostream> #include <vector> #include <cmath> #include <queue> using namespace std; struct node { int xl, xr, yl, yr; int s, t; int sum; }num[1000005]; priority_queue<int>ans; void build(int xl, int xr, int yl, int yr, int i, int cur) { num[i].xl = xl; num[i].yl = yl; num[i].xr = xr; num[i].yr = yr; num[i].sum = (xr - xl + 1) * (yr - yl + 1); num[i].s = 0; num[i].t = cur; if(xl == xr && yl == yr) return; int mx = (xl + xr)>>1; int my = (yl + yr)>>1; build(xl, mx, yl, my, 4*i - 2, 1); build(mx + 1, xr, yl, my, 4*i - 1, 2); build(xl, mx, my + 1, yr, 4*i, 3); build(mx + 1, xr, my + 1, yr, 4*i + 1, 4); } void update(int x, int y, int i) { if(num[i].xl == num[i].xr && num[i].yl == num[i].yr) { num[i].s = 1; return; } int mx = (num[i].xl + num[i].xr)>>1; int my = (num[i].yl + num[i].yr)>>1; if(x <= mx && y <= my) { update(x, y, i*4 - 2); } else if(x > mx && y <= my) { update(x, y, i*4 - 1); } else if(x <= mx && y > my) { update(x, y, i*4); } else { update(x, y, i*4 + 1); } num[i].s++; } void dfs(int i, int way, int n) { if(num[i].s == 0) return; else if(num[i].s == num[i].sum) { ans.push(-way); return; } else { dfs(i*4 - 2, way + 1*pow(10, n), n + 1); dfs(i*4 - 1, way + 3*pow(10, n), n + 1); dfs(i*4, way + 2*pow(10, n), n + 1); dfs(i*4 + 1, way + 4*pow(10, n), n + 1); } } int FtoT(int x) { x *= -1; int s = 0; int d = 0; int k; while(x) { k = x % 10; s += k*pow(5, d++); x/=10; } return s; } int main() { freopen("in.txt","r",stdin); int n, m; cin>>n; build(1, n, 1, n, 1, 0); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { cin>>m; if(m) update(i, j, 1); } } dfs(1, 0, 0); while(ans.size()) { cout<<FtoT(ans.top())<<" "; ans.pop(); } return 0; }
习题6-9,UVa127,Time:11.5
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <stack> using namespace std; struct node { char n, f; }; vector<stack<node> >pokelist; bool init() { bool flag = false; node v, v1, v3; for(int i = 0; i < pokelist.size(); i++) { int xflag = false; if(pokelist[i].size() == 0) continue; v = pokelist[i].top(); if(i >= 3) { v3 = pokelist[i-3].top(); if(v.n == v3.n || v.f == v3.f) { xflag = true; pokelist[i].pop(); pokelist[i-3].push(v); flag = true; } } if(i >= 1 && xflag == false) { v1 = pokelist[i-1].top(); if(v.n == v1.n || v.f == v1.f) { pokelist[i].pop(); pokelist[i-1].push(v); flag = true; } } if(pokelist[i].size() == 0) { pokelist.erase(pokelist.begin()+i); i--; } if(flag) return flag; } return flag; } int main() { freopen("in.txt","r",stdin); char poke[5]; node v; int size; for(int i = 1; i <= 52; i++) { stack<node>q; cin>>poke; cout<<poke<<" : "; v.f = poke[1]; v.n = poke[0]; q.push(v); pokelist.push_back(q); while(init()); } for(int i = 0; i < pokelist.size(); i++) cout<<pokelist[i].size()<<" "; cout<<endl; return 0; }
习题6-11,UVa10410,Time:11.4
#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; char cdfs[1005]; char cbfs[1005]; vector<int>dot[1005]; void dfs(char ds[], char bs[], int len) { if(len == 1) return; int cur[1005]; char dtmp[1005]; char btmp[1005]; int k, xl = 0; int a, b; for(int i = 0; i < len; i++) { cur[ds[i]-'0'] = i; } for(int i = 1; i < len; i++) { k = 0; if(cur[bs[i] - '0'] > cur[bs[i+1] - '0']) { xl = i; break; } a = bs[0] - '0'; b = bs[i] - '0'; dot[a].push_back(b); for(int j = i; j < len; j++) { if(cur[bs[j] - '0'] >= cur[bs[i] - '0'] && cur[bs[j] - '0'] < cur[bs[i + 1] - '0']) btmp[k++] = bs[j]; } btmp[k++] = 0; k = 0; for(int j = cur[bs[i] - '0']; j < cur[bs[i+1] - '0']; j++) dtmp[k++] = ds[j]; dtmp[k++] = 0; dfs(dtmp, btmp, k - 1); } a = bs[0] - '0'; b = bs[xl] - '0'; dot[a].push_back(b); k = 0; for(int j = xl; j < len; j++) if(cur[bs[j] - '0'] >= cur[bs[xl] - '0']) btmp[k++] = bs[j]; btmp[k++] = 0; k = 0; for(int j = cur[bs[xl] - '0']; j < len; j++) dtmp[k++] = ds[j]; dtmp[k++] = 0; dfs(dtmp, btmp, k - 1); } int main() { freopen("in.txt","r",stdin); int n; cin>>n; cin>>cdfs>>cbfs; dfs(cdfs, cbfs, n); for(int i = 1; i <= n; i++) { cout<<i<<" : "; for(int j = 0; j < dot[i].size(); j++) { cout<<dot[i][j]<<" "; } cout<<endl; } return 0; }
习题6-14,UVa12118,Time:11.5
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <stack> using namespace std; int main() { freopen("in.txt","r",stdin); int map[1005]; int v, e, t; while(cin>>v>>e>>t) { int x, y, z = 0; memset(map,0,sizeof(map)); for(int i = 0; i < e; i++) { cin>>x>>y; map[x]++; map[y]++; } for(int i = 1; i <= v; i++) if(map[i]%2) z++; cout<<z/2 + e - 1<<endl; } return 0; }