Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1073 

 

Your friend, a biochemistry major, tripped while carrying a tray of computer files through the lab. All of the files fell to the ground and broke. Your friend picked up all the file fragments and called you to ask for help putting them back together again. Fortunately, all of the files on the tray were identical, all of them broke into exactly two fragments, and all of the file fragments were found. Unfortunately, the files didn’t all break in the same place, and the fragments were completely mixed up by their fall to the floor. You’ve translated the original binary fragments into strings of ASCII 1’s and 0’s, and you’re planning to write a program to determine the bit pattern the files contained. Input ?The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. Input will consist of a sequence of “file fragments”, one per line, terminated by the end-of-file marker. Each fragment consists of a string of ASCII 1’s and 0’s. Output For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. Output is a single line of ASCII 1’s and 0’s giving the bit pattern of the original files. If there are 2N fragments in the input, it should be possible to concatenate these fragments together in pairs to make N copies of the output string. If there is no unique solution, any of the possible solutions may be output. Your friend is certain that there were no more than 144 files on the tray, and that the files were all less than 256 bytes in size.

Sample Input 1 011 0111 01110 111 0111 10111

Sample Output 01110111

Code Examples

#1 Code Example with C Programming

Code - C Programming

 #include <bits/stdc++.h>
using namespace std;

// count & take highest occurence between all pair that have the correct size
int main() {
    int t;
    string in;
    scanf("%d\n\n",&t);
    while(t--){
        vector < string> parts;
        int minSize = INT_MAX, maxSize = INT_MIN;
        while(getline(cin,in), !in.empty()){
            minSize = min((int)in.length(),minSize);
            maxSize = max((int)in.length(),maxSize);
            parts.push_back(in);
        }
        unordered_map < string,int> cntMap;
        string best = "";
        for(int i=0;i < parts.size();i++)
            for(int j=i+1;j < parts.size();j++)
        if(parts[i].size() + parts[j].size() == (minSize+maxSize)){
            string combine1 = parts[i]+parts[j], combine2 = parts[j]+parts[i];
            if(++cntMap[combine1] > cntMap[best]) best = combine1;
            if(++cntMap[combine2] > cntMap[best]) best = combine2;
        }
        cout << best << endl;
        if(t) cout << endl;

    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
011
0111
01110
111
0111
10111

Output

x
+
cmd
01110111
Advertisements

Demonstration


UVA Online Judge solution - 10132  -  File Fragmentation -  UVA Online Judge solution in C,C++,Java

Previous
UVA Online Judge solution - 10131 - Is Bigger Smarter UVA Online Judge solution in C,C++,Java
Next
UVA Online Judge solution - 10139 - Factovisors - UVA Online Judge solution in C,C++,Java