// File fragmentation (File fragmentation) // PC/Ultraviolet IDs: 110306/10132, popularity: C, success rate: average level: 2 // verdict: accepted // submission date: 2011-05-19 // UV Run Time: 0.012 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net # include <iostream> # include <vector> using namespace STD; // try to use file to match all fragments. Bool match (vector <string> frag, string file) {// finds the fragment that can constitute the file and deletes it from the vector. If the last vector is empty, it indicates that all fragments have been matched. While (frag. size ()> 0) {size_t head = file. find (frag [0]); size_t tail = file. find (frag [0], file. length ()-Frag [0]. length (); If (Head = string: NPOs & tail = string: NPOs) return false; string head_target, tail_target; if (Head = 0 & tail = string: NPOs) {head_target = ""; tail_target = file. substr (frag [0]. length ();} else if (Head = string: NPOs & tail = (file. length ()-Frag [0]. length () {head _ Target = file. substr (0, tail); tail_target = "";} else {head_target = file. substr (0, tail); tail_target = file. substr (frag [0]. length ();} frag. erase (frag. begin (); bool found = false; For (INT I = 0; I <frag. size (); I ++) {If (head_target = frag [I]) found = true; If (tail_target = frag [I]) found = true; if (found) {frag. erase (frag. begin () + I); break;} If (! Found) return false;} return true;} // restore the file. Void recover_file (vector <string> & frag) {// obtain the object length. Int file_length = 0; For (INT I = 0; I <frag. size (); I ++) file_length + = frag [I]. length (); file_length <= 1; file_length/= frag. size (); string head = frag [0]; vector <string> tail; // locate all the different half of the first part, note that fragment containing frag [0] may be found. If the file is // 10110111 and frag [0] = 101, the fragment with a length of 5 May Have 10110, 101, and // are both in front of the clip. For (INT I = 1; I <frag. size (); I ++) if (frag [I]. length () = (file_length-head. length () & frag [I]! = Head) {bool add = true; For (Int J = 0; j <tail. size (); j ++) if (frag [I] = tail [J]) {Add = false; break;} If (ADD) tail. push_back (frag [I]);} If (tail. size () = 0) {cout <(Head + head) <Endl; return;} // try different combinations and match all fragments. For (INT I = 0; I <tail. size (); I ++) {string file = head + tail [I]; If (MATCH (frag, file) {cout <file <Endl; return ;} file = tail [I] + head; If (MATCH (frag, file) {cout <file <Endl; return ;}} int main (INT AC, char * AV []) {INT cases; vector <string> frag; string line; CIN> cases; cin. ignore (); Getline (CIN, line); While (cases --)> 0) {frag. clear (); While (Getline (CIN, line), line. length ()> 0) frag. push_back (line); If (frag. size () = 2) cout <(frag [0] + frag [1]) <Endl; elserecover_file (frag); If (cases> 0) cout <Endl;} return 0 ;}