字符串uva 10132 File Fragmentation

uva 10132 File Fragmentation

一开始想太多,用深搜做直接超时了,换了方法过了,直接找出最长和最短,拼出得串去检查是否符合,符合输出即可

#include <stdio.h>
#include <string.h>

int t;
int tt;
int min, max;
char a[300][300];
int len;
char c[300];
char cn[300];
int judge(char *c)
{
    int cent = 0;
    for (int i = 0; i < tt; i ++)
    {
	if (strncmp(c, a[i], strlen(a[i])) == 0)
	{
	    cent ++;
	    continue;
	}
	if (strncmp(c + len - strlen(a[i]), a[i], strlen(a[i])) == 0)
	{
	    cent ++;
	    continue;
	}
    }
    if (cent != tt)
	return 1;
    else
	return 0;
}
int main()
{
    scanf("%d", &t);
    getchar();
    getchar();
    int maxx;
    while (t --)
    {
	memset(c, 0, sizeof(c));
	tt = 0; min = 999; max = 0;
	while (gets(a[tt]) != NULL && a[tt][0] != '\0')
	{
	    if (strlen(a[tt]) > max)
	    {
		max = strlen(a[tt]);
		maxx = tt;
	    }
	    if (strlen(a[tt]) < min)
		min = strlen(a[tt]);
	    tt ++;
	}
	len = max + min;
	for (int i = 0; i < tt; i ++)
	{
	    if (min == strlen(a[i]))
	    {
		strcpy(c, a[i]);
		strcpy(c + strlen(a[i]), a[maxx]);
		if (judge(c) == 0)
		{
		    printf("%s\n", c);
		    break;
		}
		strcpy(c, a[maxx]);
		strcpy(c + strlen(a[maxx]), a[i]);
		if (judge(c) == 0)
		{
		    printf("%s\n", c);
		    break;
		}
	    }
	}
	if (t > 0)
	    printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值