• uva 10132


    就是一个文件的很多副本碎成两半, 让你根据所有碎片求出原来的文件

    排序枚举一下就ok了

    题目:

    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
    


    代码:

      1 #include <iostream>
      2 #include <string>
      3 #include <cstdio>
      4 #include <algorithm>
      5 #include <memory.h>
      6 #include <set>
      7 using namespace std;
      8 
      9 
     10 string frag[200];
     11 int p;
     12 int vis[200];
     13 int len;
     14 set<string> v;
     15 int judge(string str)
     16 {
     17    // cout<<"str : "<<str<<endl;
     18     for(int i=0;i<p;i++)
     19     {
     20         for(int j=p-1;j>i;j--)
     21         {
     22             if(!vis[i] && !vis[j] && frag[i]+frag[j]==str || frag[j]+frag[i]==str)
     23             {
     24                 vis[i]=vis[j]=1;
     25             }
     26         }
     27         if(!vis[i])
     28         {
     29            // cout<<" I "<<i << " frag :" <<frag[i]<<endl;
     30             return 0;
     31         }
     32     }
     33     if(!v.count(str))
     34     {
     35         v.insert(str);
     36 
     37         cout<<str<<endl;
     38     }
     39     return 1;
     40 }
     41 
     42 int solve()
     43 {
     44     v.clear();
     45     memset(vis,0,sizeof(vis));
     46     string tmp ;
     47 
     48     len = frag[0].length()+ frag[p-1].length();
     49 
     50     for(int i=0;i<p;i++)
     51     {
     52 
     53         for(int j=p-1;j>i;j--)
     54         {
     55             int tlen = frag[i].length()+frag[j].length();
     56             if(tlen<len)
     57             {
     58                 break;
     59             }
     60 
     61             vis[i]=1;vis[j]=1;
     62 
     63             judge (frag[i]+frag[j] );
     64             memset(vis,0,sizeof(vis));
     65             judge(frag[j]+frag[i]);
     66             memset(vis,0,sizeof(vis));
     67         }
     68 
     69     }
     70 
     71 }
     72 
     73 bool cmp(string a, string b)
     74 {
     75     return a.length()<b.length();
     76 }
     77 
     78 int main()
     79 {
     80     freopen("in","r",stdin);
     81   //  freopen("out","w",stdout);
     82 
     83     int tst;
     84     cin>>tst;
     85     cin.ignore();
     86     string tmp;
     87 
     88 
     89         getline(cin,tmp);
     90     while(tst--)
     91     {
     92         p=0;
     93         while(getline(cin,tmp))
     94         {
     95             if(!tmp.empty())
     96             {
     97                 frag [p++] = tmp;
     98             }
     99             else
    100             break;
    101         }
    102 
    103         sort(frag,frag+p,cmp);
    104         solve();
    105         if(tst)cout<<endl;
    106 
    107     }
    108 
    109 
    110     return 0;
    111 }
  • 相关阅读:
    e.g.-basic-Si
    Telephone interview with Youyou Tu
    Mo2C-tag
    Usage of “symmetrical” and “symmetric”
    Xcrysden-2
    The partial charge density (1)
    利用 AWK 的数值计算功能提升工作效率(转载)
    扩展程序
    选择排序
    装饰递归函数
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3489754.html
Copyright © 2020-2023  润新知