UVA File Fragmentation(文件复原)

探讨如何通过算法解决相同文件被分成不同长度碎片的问题,利用排序和字符串拼接技术找到原始文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Download as PDF

Question 2: File Fragmentation

The Problem

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
 
     
 
     
     题意:有n份相同的文件内容全部有0和1组成,由于失误吧文件全部损坏了,每份文件都变成了残缺的两份(每一份都变成了两份),现在要你输出该文件的内容。
     分析:因为每份文件都变成了两份,那么可以认为 最小的碎片长度+最大的碎片长度 = 该文件的长度。最小的碎片+最大的碎片 的其中一个组合一定是正确的文件顺序。将所有的组合存起来然后和第二小的碎片+ 第二大的碎片的组合进行比较,相同的哪一个组合就是符合要求的一个值。
(AC了之后想想才发现这貌似是最麻烦的一种方法。)
代码:

      
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char a[201][601];
int n;

int cmp(const void* a, const void* b)
{
    return strlen((char*)a) > strlen((char*)b);
}

int main()
{
    int T;
    char ss[100];
    scanf("%d\n",&T);
    while(T--)
    {
        n = 0;
        while (gets(a[n]) && a[n][0]) ++n;
        qsort(a,n,sizeof(a[0]),cmp);
        int flag = 0;
        char str[5][501];
        char str1[5][501];
        memset(str,0,sizeof(str));
        memset(str1,0,sizeof(str1));
        strcpy(str[0],a[0]);
        strcpy(str[1],a[n-1]);
        strcat(str[0],a[n-1]);
        strcat(str[1],a[0]);
        int m = 2;
        for(int i=1; i<n/2; i++)
        {
            if(strcmp(a[i],a[0]) != 0 && strlen(a[0]) == strlen(a[i]))
            {
                strcpy(str[m],a[0]);
                strcat(str[m],a[n-i-1]);
                m++;
                strcpy(str[m],a[n-i-1]);
                strcat(str[m],a[0]);
                m++;
                strcpy(str1[0],a[i]);
                strcpy(str1[1],a[n-1-i]);
                strcat(str1[0],a[n-1-i]);
                strcat(str1[1],a[i]);
                int mm = 2;
                strcpy(str1[mm],a[i]);
                strcat(str1[mm],a[n-1]);
                mm++;
                strcpy(str1[mm],a[n-1]);
                strcat(str1[mm],a[i]);
                mm++;
                for(int pi=0; pi<m; pi++)
                {
                    for(int pj=0; pj<mm; pj++)
                    {
                        if(strcmp(str1[pj],str[pi]) == 0)
                        {
                            printf("%s\n",str[pi]);
                            flag = 1;
                            break;
                        }
                    }
                    if(flag == 1)
                    {
                        break;
                    }
                }
                break;
            }
            else if(strlen(a[i])!=strlen(a[0]))
            {
                strcpy(str1[0],a[i]);
                strcpy(str1[1],a[n-1-i]);
                strcat(str1[0],a[n-1-i]);
                strcat(str1[1],a[i]);
                int mm = 2;
                for(int j=i+1; j<n/2; j++)
                {
                    if(strlen(a[j]) == strlen(a[i]) && strcmp(a[j],a[i])!=0)
                    {
                        strcpy(str1[mm],a[i]);
                        strcpy(str1[mm],a[n-1-j]);
                        mm++;
                        strcat(str1[mm],a[n-1-j]);
                        strcat(str1[mm],a[i]);
                        break;
                    }
                }
                for(int pi=0;pi<m;pi++)
                {
                    for(int pj=0;pj<mm;pj++)
                    {
                        if(strcmp(str[pi],str1[pj]) == 0)
                        {
                            printf("%s\n",str[pi]);
                            flag = 1;
                            break;
                        }
                    }
                    if(flag == 1)
                    {
                        break;
                    }
                }
                break;
            }
        }
        if(flag == 0)
        {
            printf("%s\n",str[0]);
        }
        if(T)
        {
            printf("\n");
        }

    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值