1, string parsing a string of characters as the different slices, a slice can not be repeated, in alphabetical order output all slices (each slice row)
Input: aaabbcaaabaa
Output:
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, the Huffman tree for the Minimum Weighted Path Length
Input:
4
1 1 1 1
Output: 8
Input:
4
22 5 6 3
Output: 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 ; }