Idea: the longest fragment and the shortest fragment of the combination must have the answer. So the lengths are then combined, and the final detection of the combined result string ensures that the input fragment string is at the beginning or end of it.
Summary: Code writing side comments, when you write bad comments, it means that your thinking is not very clear, please stop to think, clear the idea. The following code is my most detailed comment, because I realized that when the program can not be AC, search to see other people do not comment on the code will be more annoying.
#include <stdio.h> #include <string.h>void Reverse (char *strdest,const char *strsrc) {int len = strlen (strsrc int i;for (i = 0;i < Len; i++) {Strdest[i] = strsrc[len-i-1];} Strdest[i] = ' + ';} int Iswholefile (char (*STR) [260],int strsize,char* strtmp)/*is strtmp the whole file*/{int i; Char *pos,*rpos;char strrvrs[260];/*reverse the str[i] in order to find in a reverse way*/char strrvrstmp[260]; for (i = 1;i <= strsize;i + +)/*if The strtmp is the whole file,then every str[i] should being find in the strtmp ' Start or E nd pos*/{pos = strstr (Strtmp,str[i]); if (NULL = = pos)/*not Find * * {return 0; if (pos! = strtmp)/*str[i] must at start POS for the string or at the end of the string*/{Reverse (strrvrst MP,STRTMP);/*reverse the Strtmp*/reverse (Strrvrs,str[i]);/*reverse the str[i]*/rpos = strstr (strrvrstmp,strrvr s);/*then here can implement the function of rfind*/if (rpos! = strrvrstmp)/*not at the end,due to the reverse operation,finding str[i] in the end of Strtmp means,in fact,rpos isthe start position Of the strrvrstmp*/{return 0; }}} return 1;} void Recoverfile (char (*STR) [260],int nmaxlen,int nminlen,int nstrsize) {int i,j; Char strrst[8][260];/*possible File*/char tmp[260]; int count = 0; for (i = 1;i <= nstrsize;i + +) {if (strlen (str[i]) = = Nmaxlen)/*find the longest string*/{ for (j = 1;j <= Nstrsize;j + +) {if (strlen (str[j]) = = Nminlen)/*find the shortest string*/ {strcpy (tmp,str[i]); strcat (Tmp,str[j]); strcpy (Strrst[count], TMP);/*recover in A-a-type long est-shorest*/count++;strcpy (Tmp,str[j]); strcat (Tmp,str[i]); strcpy (Strrst[count], TMP);/*recover in A- shortest-longest*/count++; }}}} for (i = 0;i < Count;i + +)/*there is at the most 8 posSible Answer,test Each of the them*/{if (Iswholefile (Str,nstrsize,strrst[i]))/*test Weather The string is whole fil e*/{strcpy (str[0],strrst[i]); return; }}}int Main () {char str[150][260]; int ntest,i; int Nmax_len; int nmin_len,nlength; Char input[256]; scanf ("%d", &ntest); GetChar (); GetChar (); while (Ntest-) {i = 0; Nmax_len = 0; Nmin_len = 256; strcpy (Str[0], ""); while (gets (input) && strlen (input)! = 0) {i ++;strcpy (str[i],input); Nlength = strlen (Str[i]); if (Nlength > Nmax_len)/*get the max len*/nmax_len = nlength; if (Nlength < Nmin_len)/*get the min len*/nmin_len = nlength; } if (2 = = i)/*if There is just II string,concatenate them and output*/{strcpy (Str[0],strcat (s TR[1],STR[2])); } else Recoverfile (str,nmax_len,nmin_len,i);/*rEcover the file,only use the file whose length is max or min length*/printf ("%s\n", Str[0]);/*rememner the enter*/ if (ntest) printf ("\ n");/*out put the Blank line*/} return 0;}
File Fragmentation Uva 10132