夏季学期实训-基础数据结构(栈与队列)
A:HDU 1072 (易)
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-03 09:37 5 * Filename : hdu_1072.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22 23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30 31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 const int LEN = 1001; 34 int n; 35 char op[LEN]; 36 queue<int> q; 37 stack<int> s; 38 39 void solvea(){ 40 int num; 41 scanf("%s", op); 42 if(!strcmp(op, "IN")){ 43 scanf("%d\n", &num); 44 q.push(num); 45 }else { 46 if(q.empty()) puts("None"); 47 else { 48 printf("%d\n", q.front()); 49 q.pop(); 50 } 51 } 52 } 53 54 void solveb(){ 55 int num; 56 scanf("%s", op); 57 if(!strcmp(op, "IN")){ 58 scanf("%d\n", &num); 59 s.push(num); 60 }else{ 61 if(s.empty()){ 62 puts("None"); 63 }else { 64 printf("%d\n", s.top()); 65 s.pop(); 66 } 67 } 68 } 69 70 int main() 71 { 72 // freopen("in.txt", "r", stdin); 73 74 int T; 75 char con[LEN]; 76 scanf("%d", &T); 77 while(T--){ 78 while(!s.empty()) s.pop(); 79 while(!q.empty()) q.pop(); 80 scanf("%d%s", &n, con); 81 for(int i=0; i<n; i++){ 82 if(!strcmp(con, "FIFO")) solvea(); 83 else solveb(); 84 } 85 } 86 return 0; 87 }
B:HDU 1022(易)
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-03 09:53 5 * Filename : hdu_1022.cpp 6 * Description : 7 * ************************************************/ 8 #include <iostream> 9 #include <cstdio> 10 #include <cstring> 11 #include <cstdlib> 12 #include <cmath> 13 #include <algorithm> 14 #include <queue> 15 #include <stack> 16 #include <vector> 17 #include <set> 18 #include <map> 19 #define MP(a, b) make_pair(a, b) 20 #define PB(a) push_back(a) 21 22 using namespace std; 23 typedef long long ll; 24 typedef pair<int, int> pii; 25 typedef pair<unsigned int,unsigned int> puu; 26 typedef pair<int, double> pid; 27 typedef pair<ll, int> pli; 28 typedef pair<int, ll> pil; 29 30 const int INF = 0x3f3f3f3f; 31 const double eps = 1E-6; 32 const int LEN = 100010; 33 34 int I(char c){return c-'0';} 35 36 int main() 37 { 38 // freopen("in.txt", "r", stdin); 39 40 stack<int> s; 41 queue<int> q; 42 char in[11], out[11]; 43 int ta, tb, n; 44 while(scanf("%d", &n)!=EOF){ 45 while(!s.empty()) s.pop(); 46 while(!q.empty()) q.pop(); 47 scanf("%s%s", &in, &out); 48 ta = tb = 0; 49 int ans = 1; 50 while(1){ 51 if(s.empty()) { 52 s.push(I(in[ta++])); 53 q.push(0); 54 }else if(s.top() == I(out[tb])){ 55 tb ++; s.pop(); 56 q.push(1); 57 if(tb == n) break; 58 }else { 59 if(ta == n) { 60 ans = 0; 61 break; 62 }else{ 63 q.push(0); 64 s.push(I(in[ta++])); 65 } 66 } 67 } 68 if(!ans) puts("No."); 69 else{ 70 puts("Yes."); 71 while(!q.empty()){ 72 if(q.front() == 0) puts("in"); 73 else puts("out"); 74 q.pop(); 75 } 76 } 77 puts("FINISH"); 78 } 79 return 0; 80 }
C: HDU 1387(中等)
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-03 10:15 5 * Filename : hdu_1387.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22 23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30 31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 const int LEN = 1010; 34 int n, kase = 1; 35 map<int, int> mp; 36 queue<int> qa, q[LEN]; 37 38 void init(){ 39 for(int i=0; i<LEN; i++) 40 while(!q[i].empty()) q[i].pop(); 41 mp.clear();while(!qa.empty())qa.pop(); 42 printf("Scenario #%d\n", kase ++); 43 } 44 45 int main() 46 { 47 // freopen("in.txt", "r", stdin); 48 49 int num, tn; 50 char op[LEN]; 51 while(scanf("%d", &n)!=EOF && n){ 52 init(); 53 for(int i=0; i<n; i++){ 54 scanf("%d", &tn); 55 for(int j=0; j<tn; j++){ 56 scanf("%d", &num); 57 mp[num] = i; 58 } 59 } 60 while(scanf("%s", op)){ 61 if(!strcmp(op, "STOP")) break; 62 else if(!strcmp(op, "ENQUEUE")) { 63 scanf("%d", &num); 64 int pos = mp[num]; 65 if(q[pos].empty()) qa.push(pos); 66 q[pos].push(num); 67 }else if(!strcmp(op, "DEQUEUE")){ 68 int pos = qa.front(); 69 if(q[pos].empty()) qa.pop(); 70 pos = qa.front(); 71 printf("%d\n", q[pos].front()); 72 q[pos].pop(); 73 } 74 } 75 puts(""); 76 } 77 return 0; 78 }
E:HDU 1873(易)
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-03 12:36 5 * Filename : hdu_1873.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22 23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30 31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 const int LEN = 11; 34 queue<int> q[LEN][LEN]; 35 int n; 36 37 int main() 38 { 39 // freopen("in.txt", "r", stdin); 40 41 char str[LEN]; 42 int a, b; 43 while(scanf("%d", &n)!=EOF){ 44 for(int i=0; i<LEN; i++) 45 for(int j=0; j<LEN; j++) 46 while(!q[i][j].empty()) q[i][j].pop(); 47 int top = 0; 48 for(int i=0; i<n; i++){ 49 scanf("%s", str); 50 if(!strcmp(str, "IN")){ 51 scanf("%d%d", &a, &b); 52 q[a][b].push(++top); 53 }else{ 54 scanf("%d", &a); 55 int ans = 0; 56 for(int j=LEN-1; j>0; j--){ 57 if(q[a][j].empty()) continue; 58 ans = 1; 59 printf("%d\n", q[a][j].front()); 60 q[a][j].pop(); 61 break; 62 } 63 if(!ans) puts("EMPTY"); 64 } 65 } 66 } 67 return 0; 68 }
F:HDU1509(中等)
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-03 17:46 5 * Filename : hdu_1059.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22 23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30 31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 const int LEN = 1010; 34 35 struct Mes{ 36 char s[51]; 37 int m, p, tag; 38 }; 39 40 struct cmp{ 41 bool operator() (Mes a, Mes b){ 42 if(a.p == b.p) return a.tag > b.tag; 43 else return a.p > b.p; 44 } 45 }; 46 47 int main() 48 { 49 // freopen("in.txt", "r", stdin); 50 51 priority_queue<Mes, vector<Mes>, cmp> q; 52 char str[LEN]; 53 int top = 1; 54 while(!q.empty()) q.pop(); 55 while(scanf("%s", str)!=EOF){ 56 if(!strcmp(str, "PUT")){ 57 Mes tp; 58 scanf("%s%d%d", tp.s, &tp.m, &tp.p); 59 tp.tag = top++; 60 q.push(tp); 61 }else{ 62 if(q.empty()) puts("EMPTY QUEUE!"); 63 else { 64 printf("%s %d\n", q.top().s, q.top().m); 65 q.pop(); 66 } 67 } 68 } 69 return 0; 70 }
G:HDU 1870(易)
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-03 18:34 5 * Filename : hdu_1870.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22 23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30 31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 const int LEN = 100010; 34 char str[LEN]; 35 36 int main() 37 { 38 // freopen("in.txt", "r", stdin); 39 40 while(scanf("%s", &str)!=EOF){ 41 int len = strlen(str); 42 int ans = 0; 43 for(int i=0; i<len; i++){ 44 if(str[i] == '(') ans++; 45 else if(str[i] == ')') ans--; 46 else break; 47 } 48 printf("%d\n", ans); 49 } 50 return 0; 51 }
I:POJ 3253 (中等)
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-13 11:43 5 * Filename : poj_3253.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22 23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30 31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 priority_queue<int, vector<int>, greater<int> > q; 34 int n; 35 36 int main() 37 { 38 // freopen("in.txt", "r", stdin); 39 40 while(cin >> n){ 41 while(!q.empty()) q.pop(); 42 for(int i=0; i<n; i++){ 43 int num; 44 cin >> num; 45 q.push(num); 46 } 47 ll ans = 0; 48 while(q.size() > 1){ 49 int a = q.top(); q.pop(); 50 int b = q.top(); q.pop(); 51 ans += (a + b); 52 q.push(a + b); 53 } 54 cout << ans << endl; 55 } 56 return 0; 57 }
J:POJ 2312 (较难)
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-13 10:22 5 * Filename : poj_2312.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22 23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30 31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 const int LEN = 310; 34 char Map[LEN][LEN]; 35 int m, n, vis[LEN][LEN], sx, sy; 36 int xx[] = {0, 0, 1,-1}; 37 int yy[] = {1,-1, 0, 0}; 38 struct P{ 39 int x, y, st; 40 }; 41 42 struct cmp{ 43 bool operator() (P a, P b){ 44 return a.st > b.st; 45 } 46 }; 47 48 P mpp(int _x, int _y, int _st){ 49 P ret; 50 ret.x = _x;ret.y = _y;ret.st = _st; 51 return ret; 52 } 53 54 bool J(int x, int y){ 55 return x >= 0 && x < n && y >= 0 && y < m && Map[x][y] != 'S' && Map[x][y] != 'R'; 56 } 57 58 int bfs(){ 59 priority_queue<P, vector<P>, cmp> q; 60 memset(vis, 0, sizeof vis); 61 vis[sx][sy] = 1; 62 q.push(mpp(sx, sy, 0)); 63 while(!q.empty()){ 64 P nvex = q.top(); q.pop(); 65 int x = nvex.x, y = nvex.y, st = nvex.st; 66 if(Map[x][y] == 'T') return st; 67 for(int i=0; i<4; i++){ 68 int tx = x + xx[i]; 69 int ty = y + yy[i]; 70 int nst = st + 1; 71 if(J(tx, ty) && !vis[tx][ty]){ 72 if(Map[tx][ty] == 'B') nst ++; 73 vis[tx][ty] = 1; 74 q.push(mpp(tx, ty, nst)); 75 } 76 } 77 } 78 return -1; 79 } 80 81 int main() 82 { 83 // freopen("in.txt", "r", stdin); 84 85 while(cin >> n >> m){ 86 if(!n && !m) break; 87 for(int i=0; i<n; i++){ 88 for(int j=0; j<m; j++){ 89 cin >> Map[i][j]; 90 if(Map[i][j] == 'Y'){ 91 sx = i; sy = j; 92 } 93 } 94 } 95 int ans = bfs(); 96 cout << ans << endl; 97 } 98 return 0; 99 }
奔跑吧!少年!趁着你还年轻