File Fragmentation Uva 10132

思路:最长的碎片和最短的碎片的组合中肯定有答案。所以先求长度,然后组合他们,最后检测组合结果字符串是否确保输入的碎片字符串都在其开始或结尾处。

总结:代码编写边注释,当你注释写不好的时候,说明你的思路也不是很清晰,请停下来思考,理清思路。下面这段代码是我注释得最详细的,因为我体会到,当自己写的程序不能AC的时候,搜搜看看别人没有注释的代码会更加让自己烦躁。

#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] = '\0';
}
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 be 
									find in the strTmp'start or end pos*/ 
    {							
        pos = strstr(strTmp,str[i]);
        if(NULL == pos )/*not find */
        {
            return 0;
        }
        if(pos != strTmp )/*str[i] must at start pos of the string or at the end of the string*/
        {
			Reverse(strRvrsTmp,strTmp);/*reverse the strTmp*/
			Reverse(strRvrs,str[i]);/*reverse the str[i]*/
            rpos = strstr(strRvrsTmp,strRvrs);/*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 way longest-shorest*/
					count++;

					strcpy(tmp,str[j]);
					strcat(tmp,str[i]);
					strcpy(strRst[count] ,tmp);/*recover in a way shortest-longest*/
					count++;
                    
                }
            }
        }
    }

   for(i = 0;i < count;i ++)/*there are at most 8 possible answer,test each of them*/
    {
        if(IsWholeFile(str,nStrSize,strRst[i]))/*test weather the string is whole file*/
        {
			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 are just two string,concatenate them and output*/
        {
            strcpy(str[0],strcat(str[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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UDP分片是指将一个过大的UDP数据报分割成多个IPv4片段的过程。UDP分片卸载允许设备将超大的UDP数据报分割成多个IPv4片段。与TCP分段卸载的要求类似,但是对于分片后的IPv4片段,其IPv4标识符不应该递增,因为它们属于同一个UDP数据报的分片。\[1\] 关于UDP分片卸载的更多信息可以参考文档\[2\]。在链路层中,有一个最大传输单元(MTU)的概念,它限制了数据帧的最大长度。不同网络类型的MTU值不同,例如以太网的MTU是1500字节。当IP层需要传输的数据包长度超过MTU时,就需要对数据包进行分片操作,使每个分片的长度小于或等于MTU。UDP分片就是在这种情况下发生的。\[3\] 总结来说,UDP分片是将超大的UDP数据报分割成多个IPv4片段的过程,以适应链路层的MTU限制。每个分片都包含有关它们属于同一个UDP数据报的信息。 #### 引用[.reference_title] - *1* *2* [Kernel: net: udp: ufo,UDP fragment offload, UDP分片脱负](https://blog.csdn.net/qq_36428903/article/details/126394978)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [UDP可靠传输(KCP))](https://blog.csdn.net/asdaqqwc/article/details/122385671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值