Accepted丶 Personal Training (数据结构 && STL) 【未完待续】
2016-06-12 22:47
363 查看
Problem A ZOJ 1004 Anagrams by Stack
题意:给你两个串,一个原串,一个目标串,让你借用栈来得出所有的使原串变成目标串的序列。
解法:借用DFS递归求解,先进栈,后出栈,当出入栈次数刚好为原单词的长度时,目标单词构造完毕,输出操作序列
Problem B ZOJ 1094 Matrix Chain Multiplication
题意:给定你n个矩阵的名称和行数、列数,再给定你指定的矩阵相乘的表达值,让你用栈算出这个表达式中矩阵乘法的次数,不合法输出
分析:计算过程分为以下三种情况:
1)左括号:不影响结果,直接跳过
2)右括号:弹出栈顶的两个矩阵,计算他们的乘积,如果不能相乘,输出error,否则将新得到的矩阵压人栈中。
3)矩阵:压入栈中
Problem C ZOJ 1011 NTA
Problem D ZOJ 1062 Trees Made to Order
Problem E ZOJ 1097 Code the Tree
Problem F ZOJ 1156 Unscrambling Images
Problem G ZOJ 1167 Trees on the Level
Problem H ZOJ 1016 Parencodings
Problem I ZOJ 1944 Tree Recovery
Problem J ZOJ 2104 Let the Balloon Rise
题意:给你两个串,一个原串,一个目标串,让你借用栈来得出所有的使原串变成目标串的序列。
解法:借用DFS递归求解,先进栈,后出栈,当出入栈次数刚好为原单词的长度时,目标单词构造完毕,输出操作序列
#include <bits/stdc++.h> #define INF 0x3f3f3f3f #define eps 1e-8 typedef long long LL; const double pi = acos(-1.0); const long long mod = 1e9 + 7; using namespace std; string a,b; stack <char> build; vector <char> operate; int len; void dfs(int ipush,int ipop) { if(ipush == len && ipop == len) { for(int i = 0;i < operate.size();i++) cout << operate[i] << " "; cout << endl; } if(ipush < len) { build.push(a[ipush]); operate.push_back('i'); dfs(ipush + 1,ipop); build.pop(); operate.pop_back(); } if(ipop < len && ipop < ipush && build.top() == b[ipop]) { char tc = b[ipop]; build.pop(); operate.push_back('o'); dfs(ipush,ipop + 1); build.push(tc); operate.pop_back(); } } int main() { //freopen("int.txt","r",stdin); //freopen("out.txt","w",stdout); while(cin >> a >> b){ len = a.length(); cout << "[" << endl; dfs(0,0); cout << "]" << endl; } return 0; }
Problem B ZOJ 1094 Matrix Chain Multiplication
题意:给定你n个矩阵的名称和行数、列数,再给定你指定的矩阵相乘的表达值,让你用栈算出这个表达式中矩阵乘法的次数,不合法输出
"error"
分析:计算过程分为以下三种情况:
1)左括号:不影响结果,直接跳过
2)右括号:弹出栈顶的两个矩阵,计算他们的乘积,如果不能相乘,输出error,否则将新得到的矩阵压人栈中。
3)矩阵:压入栈中
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define eps 1e-8
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
struct Node
{
int row,col;
};
int main()
{
//freopen("int.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
char name;
cin >> n;
map <char,Node> m;
for(int i = 0;i < n;i++){
cin >> name;
cin >> m[name].row;
cin >> m[name].col;
}
string exp;
while(cin >> exp)
{
int i;
int ans = 0;
stack <Node> Array;
for(i = 0;i < exp.size();i++)
{
if(exp[i] == '(')
continue;
if(exp[i] == ')')
{
Node b = Array.top();
Array.pop();
Node a = Array.top();
Array.pop();
if(a.col != b.row)
{
cout << "error"<< endl;
break;
}
ans += a.row * b.row * b.col;
Node tmp = {a.row,b.col};
Array.push(tmp);
}
else
Array.push(m[exp[i]]);
}
if(i == exp.size())
cout << ans << endl;
}
return 0;
}
Problem C ZOJ 1011 NTA
Problem D ZOJ 1062 Trees Made to Order
Problem E ZOJ 1097 Code the Tree
Problem F ZOJ 1156 Unscrambling Images
Problem G ZOJ 1167 Trees on the Level
Problem H ZOJ 1016 Parencodings
Problem I ZOJ 1944 Tree Recovery
Problem J ZOJ 2104 Let the Balloon Rise
相关文章推荐
- 数据结构与算法之八 队列
- 数据结构与算法之八 队列
- 数据结构与算法之八 队列
- [数据结构]Graph之深度优先遍历(DFT)及广度优先遍历(BFT)
- 数据结构之栈和队列
- 数据结构之栈和队列
- 数据结构之栈和队列
- OpenCV基础篇之Mat数据结构
- 数据结构之队列
- 数据结构之栈
- 数据结构之链表
- 数据结构与算法:数组(二)
- 经典算法与数据结构的c++实现——带头结点的单链表
- 数据结构(线段树):BZOJ 1103 [POI2007]大都市meg
- 数据结构(线段树):CodeForces 145E Lucky Queries
- 数据结构(C语言)-单向链表
- 数据结构与算法分析(8)表、栈和队列(三)
- 循环队列
- UVA - 247 - Calling Circles(floyd算法)
- 程序员必须知道的10个算法和数据结构有哪些?