1、题目名称:影分身
时间限制:1000ms
内存限制:256M
题目描述:
已知字符串str。字符串str包含字符’x’,’y’。 如果相邻的两个字符不同,消除两个字符,优先从左边进行消除。 xyyx - > yx ->
输入描述:输入多个字符。(1<=len<=1e5)
输出描述:输出最后的分身
示例1
输入 xyyyy
输出 yyy
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stack>
using namespace std;
string solution(string str){
string result;
stack<char> stack;
for (int i = 0; i < str.size(); i++) {
char ch = str[i];
if (stack.empty() || stack.top() == ch) {
stack.push(ch);
} else {
stack.pop();
}
}
while (!stack.empty()) {
result = stack.top() + result;
stack.pop();
}
return result;
}
int main() {
string str;
getline(cin, str);;
string result = solution(str);
cout<<result<<endl;
return 0;
}
2、题目名称:寻找宝藏山
时间限制:1000ms
内存限制:256M
题目描述
一天,你去了一个神秘的森林,在那里你遇到了一个神秘的老人。他告诉你,森林里有一座宝藏山,但是要想到达宝藏山,你必须先通过一些森林的入口。 这个老人给你了一张地图,地图上有若干个入口和宝藏山,每个入口都有一个花费。你可以从任意一个入口开始,然后经过一些入口,最终到达宝藏山。但是你有一个限制,你只能走 $K$ 步,如果走的步数超过了 $K$,那么你就无法到达宝藏山,也就无法获得宝藏。 你必须实现一个程序,接受用户输入的地图信息,并计算出,你能够到达宝藏山的最小花费。
输入描述:
第一行包含三个整数 $N$、$M$ 和 $K$,表示入口的个数、边的个数和最多走的步数。 接下来 $M$ 行,每行包含三个整数 $A$、$B$ 和 $C$,表示一条从 $A$ 到 $B$ 的有向边,边权为 $C$。
输出描述:
输出一行,包含一个整数,表示你能够到达宝藏山的最小花费。
示例
示例1
输入5 10 6
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
5 2 3
2 3 4
2 1 1
2 3 4
3 4 5
输出 1061109567
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <limits>
#include <queue>
using namespace std;
int solution(int n, int m, int k, std::vector<std::vector<std::string>>& edges) {
// 创建邻接矩阵表示图
std::vector<std::vector<int>> graph(n, std::vector<int>(n, std::numeric_limits<int>::max()));
for (int i = 0; i < m; ++i) {
int a = std::stoi(edges[i][0]) - 1;
int b = std::stoi(edges[i][1]) - 1;
int c = std::stoi(edges[i][2]);
graph[a][b] = c;
}
// 使用动态规划求解最小花费
std::vector<std::vector<int>> dp(k + 1, std::vector<int>(n, std::numeric_limits<int>::max()));
dp[0][0] = 0;
for (int i = 1; i <= k; ++i) {
for (int j = 0; j < n; ++j) {
for (int l = 0; l < n; ++l) {
if (graph[l][j] != std::numeric_limits<int>::max()) {
dp[i][j] = std::min(dp[i][j], dp[i - 1][l] + graph[l][j]);
}
}
}
}
// 找到能够在步数限制内到达宝藏山的最小花费
int minCost = std::numeric_limits<int>::max();
for (int i = 0; i <= k; ++i) {
minCost = std::min(minCost, dp[i][n - 1]);
}
return minCost == std::numeric_limits<int>::max() ? -1 : minCost;
}
int main() {
int n;
int m;
int k1;
vector<vector<string>> k;
cin >> n;
cin >> m;
cin >> k1;
string line, token;
for (size_t i = 0; i < m; i++) {
vector<string> s;
getline(cin >> ws, line);
stringstream tokens(line);
while (getline(tokens, token, ' ')) {
s.push_back((token));
}
k.push_back(s);
}
int result = solution(n, m, k1, k);
cout << result << endl;
return 0;
}
3、题目名称:贝博士的机械零件时间限制:1000ms内存限制:256M
题目描述
贝博士是个大忙人,他在设计和制造一台非常复杂的机械式计算机。 最近贝博士有一点烦恼,因为机械零件的种类繁多,磨损又快,经费不太够用了。不过,他发现有一些机械零件在磨损以后,可以用若干同一型号的磨损旧零件重新回炉熔化以后再铸造出一个该型号的新零件,符合这样的特点的机械零件称为可翻新零件。 于是贝博士请来了他的助手艾小姐,请她统计一下有多少种型号的可翻新零件,每一种目前有多少存量又能以多少个旧零件重新回炉铸造出一个新零件,要求计算出对应于每一种机械零件他最终能使用的零件个数。
输入描述:
第一行输入两个数p和q,表示某型号的零件存量为p,且可以用q个旧零件重新回炉铸造出一个新零件。
输出描述:
每一种可翻新机械零件最终可以使用的个数。
示例
示例1
输入6 6
输出7
#include <iostream>
using namespace std;
int main() {
int p, q;
cin >> p >> q;
int count = p;
while (p >= q) {
int newParts = p / q;
count += newParts;
p = newParts + p % q;
}
cout << count << endl;
return 0;
}