您的位置:首页 > 理论基础 > 数据结构算法

Accepted丶 Personal Training (数据结构 && STL) 【未完待续】

2016-06-12 22:47 363 查看
Problem A ZOJ 1004 Anagrams by Stack

题意:给你两个串,一个原串,一个目标串,让你借用栈来得出所有的使原串变成目标串的序列。

解法:借用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: