uva10132 - File Fragmentation

题意:
一个字符串,有一堆副本,碎成两段,要你找原字符串。
(副本最后是原字符串的一部分)
思路:
代码写得有点乱,首先字符串碎成两段,我们要找到这两段拼起来再验证副本们是否能全部成为它的子串,找这两段的话应该找最长+最短的那个串。把最长串们放在large数组,最短串们放在small数组,然后枚举,直到验证成功。
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 150;
const int M = 260;
char str[N][M];
char small[N][M], large[N][M];
int n;
int Judge(char word[]) {
    for (int i = 0; i < n; i++) {
        if (!strstr(word, str[i]))
            return false;
    }
    return true;
}
int main() {
    int cas;
    scanf("%d\n", &cas);
    while (cas--) { 
        n = 0;
        while (gets(str[n]) && (str[n][0] == '0' || str[n][0] == '1'))
            n++;
        int s = 0x3f3f3f3f, l = -1, len;
        for (int i = 0; i < n; i++) {
            len = strlen(str[i]);
            if (len < s)
                s = len;
            if (len > l)
                l = len;
        }
        int sn = 0, ln = 0;
        for (int i = 0; i < n; i++) {
            len = strlen(str[i]);
            if (len == s) {
                strcpy(small[sn++], str[i]);
            }
            if (len == l) {
                strcpy(large[ln++], str[i]);
            }
        }
        char word[M];
        int ok;
    /*  printf("s:%d\n", s);
        for (int i = 0; i < ln; i++)
            printf("%s\n", large[i]);
        for (int i = 0; i < sn; i++)
            printf("%s\n", small[i]);*/
        for (int i = 0; i < ln; i++) {
            for (int j = 0; j < sn; j++) {
                ok = 0;
                memset(word, 0, sizeof(word));
                strcpy(word, large[i]);
                strcat(word, small[j]);
                if (Judge(word)) {
                    ok = 1;
                    break;
                }
                memset(word, 0, sizeof(word));
                strcat(small[j], word);
                if (Judge(small[j])) {
                    strcpy(word, small[j]);
                    ok = 1;
                    break;
                }
            }
            if (ok)
                break;
        }
        if (ok)
            printf("%s\n", word);
        if (cas)
            printf("\n");
    }
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值