programming-challenges File Fragmentation (110306) 题解

开始一直有错误原因在于疏忽了文件片断并不对称,所以需要尝试片断1加片断2和片断2加片断1两种结果。但是我的解法不够简明,在网上看到一个好的解法,一起贴在下面:

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

struct Frag {
	string value; 
	Frag(string v) : value(v) {}
};
bool operator<(Frag f1, Frag f2) {
	return f1.value.size() < f2.value.size();
}

bool solution(vector<Frag> &vf, int len, int pos, vector<bool> &visited, string &s) {
	if (pos == vf.size()) return true;
	int beg = pos; pos = -1; 
	for (int i = beg; i < vf.size(); i++) {
		if (!visited[i]) {
			pos = i; break;
		}
	}
	if (pos == -1) return true;
	visited[pos] = true;
	for (int i = pos + 1; i < vf.size(); i++) {
		if (visited[i] || vf[pos].value.size() + vf[i].value.size() != len) continue;
		if (pos == 0) {
			s = vf[pos].value + vf[i].value;
			visited[i] = true;
			if (solution(vf, len, pos + 1, visited, s)) return true;
			s = vf[i].value + vf[pos].value;
			if (solution(vf, len, pos + 1, visited, s)) return true;
			visited[i] = false;
		}
		else if (((vf[pos].value + vf[i].value) == s) || (vf[i].value + vf[pos].value) == s) {
			visited[i] = true;
			if (solution(vf, len, pos + 1, visited, s)) return true;
			visited[i] = false;
		}

	}
	visited[pos] = false;
	return false;
}

int main() {
	int TC = 0; cin >> TC; cin.get(); cin.get();
	bool blank = false;
	for (int tc = 0; tc < TC; tc++) {
		vector<Frag> vf; 
		string s; 
		while (getline(cin, s) && !s.empty()) {
			vf.push_back(Frag(s));
		}

		sort(vf.begin(), vf.end());
		if (blank) cout << endl;
		blank = true;
		vector<bool> visited(vf.size(), false);
		
		solution(vf, vf[0].value.size() + vf.back().value.size(), 0, visited, s);
		cout << s << endl; 
	}

	return 0;
}

网上的解法:

#include <iostream>
#include <sstream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main()
{  
    string s;
    getline(cin, s);
    size_t T;
    istringstream ss(s);
    ss >> T;
    // Skip the first empty line.
    getline(cin, s);
    while ( T-- )
    {       
        vector<string> fragments;
        while (getline(cin, s) && !s.empty())
        {
            ss.clear();
            ss.str(s);
            string fragment;
            ss >> fragment;
            fragments.push_back(fragment);
        }
        
        // Consider all concatenations of any two strings.
        map<string, int> memo;
        for (size_t i = 0; i < fragments.size(); ++i)
            for (size_t j = i + 1; j < fragments.size(); ++j)
            {
                ++memo[fragments[i] + fragments[j]];
                ++memo[fragments[j] + fragments[i]];
            }
        
        // Search for the string of highest count.
        map<string, int>::iterator iter(memo.begin());
        map<string, int>::iterator file(memo.begin());
        for (; iter != memo.end(); ++iter)
        {
            if (iter->second > file->second)
                file = iter;
        }
        cout << file->first << endl;
        if (T)
            cout << endl;
    }
    return 0;
}


你可能感兴趣的:(Algorithm,C++)