题意:
一个字符串,有一堆副本,碎成两段,要你找原字符串。
(副本最后是原字符串的一部分)
思路:
代码写得有点乱,首先字符串碎成两段,我们要找到这两段拼起来再验证副本们是否能全部成为它的子串,找这两段的话应该找最长+最短的那个串。把最长串们放在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;
}