1、字符串解析将字符串看成不同的字符切片,切片不可重复,按字母序输出所有切片(每个切片一行)
输入: aaabbcaaabaa
输出:
aa
aaa
b
bb
c
#include <iostream>
#include <set>
using namespace std;
int main() {
string s;
while (cin >> s) {
set<string> ss;
string str;
int i;
for (i = 0; i < s.length() - 1; i++) {
str += s[i];
if (s[i] != s[i + 1]) {
ss.insert(str);
str = "";
}
}
str += s[i];
ss.insert(str);
for (auto it = ss.begin(); it != ss.end(); it++)
cout << *it << endl;
}
return 0;
}
2、哈弗曼树求最小带权路径长度
输入:
4
1 1 1 1
输出: 8
输入:
4
22 5 6 3
输出: 76
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, i, sum;
vector<int> v;
while (cin >> n) {
int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
v.push_back(a[i]);
}
sort(v.begin(), v.end());
sum = 0;
while (v.size() != 1) {
sum += (v[0] + v[1]);
v.push_back(v[0] + v[1]);
v.erase(v.begin());
v.erase(v.begin());
sort(v.begin(), v.end());
}
cout << sum << endl;
}
return 0;
}