UVA File Fragmentation(文件复原)

Description

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;
}
时间: 2024-10-10 06:21:10

UVA File Fragmentation(文件复原)的相关文章

UVA 10132-File Fragmentation(map还原字符串)

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 h

File Fragmentation

很弱的数据,随便搞 #include <iostream> #include <cstdio> #include <map> #include <string> #include <cstring> #include <vector> #include <algorithm> using namespace std; void debug() { cout<<"yes"<<end

Linux CentOS 7 中find命令、三个Time、快捷键及file判断文件类型

一. find命令 locate 查找命令,从本地生成的数据库中查找文件 如果没有locate命令,安装软件包:mlocate [[email protected]_46_188_centos ~]# which locate /usr/bin/locate [[email protected]_46_188_centos ~]# rpm -qf /usr/bin/locatemlocate-0.26-5.el7.x86_64 [[email protected]_46_188_centos ~

ajax input file 提交文件

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>        <title>Html5 Ajax 上传文件</ti

File / Directory 文件的操作及远程下载

//文件远程下载 WebClient client = new WebClient();    Uri uri = new Uri(url);    client.DownloadFile(Uri uri,String filename); //文件一定得存在不然会报错所以在删除文件时先判断这个文件是否存在 File.Exists(filePath) File.Delete(filePath); //File对文件的操作 File.Move(sourceFile,destFile);//文件的移

java学习一目了然&mdash;&mdash;File类文件处理

java学习一目了然--File类文件处理 File类(java.io.File) 构造函数: File(String path) File(String parent,String child) File(File parent,String child) 创建文件: boolean createNewFile(); 创建文件夹: boolean mkdir(); 用于创建一层未定义文件夹 boolean mkdirs(); 用于创建多层未定义文件夹,相当于多个mkdir() 删除文件/文件夹

SQL Server -&gt;&gt; Sparse File(稀疏文件)

Sparse File(稀疏文件)不是SQL Server的特性.它属于Windows的NTFS文件系统的一个特性.如果某个大文件中的数据包含着大量“0数据”(这个应该从二进制上看),这样的文件就可以被称之为稀疏文件.如果从二维图上看这个文件你会发现文件就像很多很多洞一样,这就是“稀疏”的由来.这种文件造成的问题是空间浪费.比如说如果你现在用VMWare Workstatation创建了一个虚拟机,初始化磁盘大小为40G,VM必然会为虚拟机生成一个或者多个.vmdk文件.如果文件系统真的分配40

Java——File(文件)

 public static void main(String[] args) { // getFile(); /* * 需求:  对指定目录进行所有内容的列出,(包含子目录中的内容) * */ File dir = new File("E:\\HB_JAVA解压"); listAll(dir, 0); } public static void listAll(File dir, int len) { System.out.println(getSpace(len) + dir.g

GRT Recover My File误删文件恢复

在日常的电脑操作中,我们可能会由于硬盘硬件损坏.硬盘格式化导致文件的丢失,在这种情况下,推荐一款误删文件的恢复软件给大家使用. GRT Recover My File是由GRTSoft公司推出的一款数据恢复软件,能够帮助你恢复误删的照片.电影.歌曲等资源. 功能介绍:1.GRT Recover My File支持FAT12.FAT16.FAT32.NTFS文件系统:2.可以帮助你快速的恢复被误删除的文件和文件夹,即使回收站已被清空或使用SHIFT + Del键彻底删除也可以恢复:3.程序提供了易