Image Service (Search) 10132 file fragmentation)

Source: Internet
Author: User

Question connection: 10132 file fragmentation


There are n files consisting of 0 and 1, each of which is randomly divided into two parts. Now all 2n fragments are given, and the original sequence must be found.


Solution: search for the right two ends. The shortest must match the longest.

#include <stdio.h>#include <string.h>#include <string>#include <algorithm>#include <iostream>using namespace std;const int N = 300;char str[N];string file[N];int vis[N], len, n;string order;bool cmp(const string &a, const string &b) {    return a.length() < b.length();}bool solved(int cur) {    if (cur >= n / 2)return true;    vis[cur] = 1;    int l = file[cur].length();    for (int i = n - 1; i >= 0; i--) {if (vis[i])   continue;if (l + file[i].length() > len)   return true;if (l + file[i].length() < len)   return false;string p, q;p = file[cur] + file[i];q = file[i] + file[cur];if (p == order || q == order) {    vis[i] = 1;    if (solved(cur + 1))return true;    vis[i] = 0;}    }    vis[cur] = 0;    return false;}int main() {    int cas, sum, max;    scanf("%d%*c%*c", &cas);    while (cas--) {sum = n = 0;memset(vis, 0, sizeof(vis));while (gets(str)) {    if (!str[0])break;    file[n] = str;    sum += file[n++].length();}sort(file, file + n, cmp);len = sum * 2 / n;vis[0] = 1;for (int i = n - 1; i >= 0; i--) {    vis[i] = 1;    order.clear();    order = file[0] + file[i];    if (solved(1))  break;    order.clear();    order = file[i] + file[0];    if (solved(1))  break;    vis[i] = 0;}cout << order << endl;if (cas)    cout << endl;    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.