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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值