C ++(Bcc),287个字节
#include<algorithm.h>
f(a,b)char*a,**b;{int i,j,k,v,p[256];if(!a||!b||!*b)return-1;for(v=0;v<256&&b[v];++v)p[v]=v;if(v>=256)return-1;la:for(i=0,j=0;j<v&&a[i];){for(k=0;b[p[j]][k]==a[i]&&a[i];++i,++k);j=b[p[j]][k]?(i-=k),j+1:0;}if(a[i]&&next_permutation(p,p+v)) goto la;return i&&!a[i];}
因为我没有写或使用过多的next_permutation()我不知道是否还可以。我不知道这是否是解决方案的100%,也可能是质量过高了。这里的一个字符串列表是一个指向char的指针数组。以NULL结尾的算法很简单,如果列表中的所有字符串都与参数“ a”匹配,则有一个线性尝试的算法,还有另一个算法会置换字符串列表的索引,因此它尝试所有可能的组合。
解开它,在这里测试代码和结果
#include<stdio.h>
g(a,b)char*a,**b;
{int i,j,k,v,p[256];
if(!a||!b||!*b) return -1;
for(v=0;v<256&&b[v];++v) p[v]=v;
if(v>=256) return -1; // one array of len >256 is too much
la:
for(i=0,j=0;j<v&&a[i];)
{for(k=0;b[p[j]][k]==a[i]&&a[i];++i,++k);
j=b[p[j]][k]?(i-=k),j+1:0;
}
if(a[i]&&next_permutation(p,p+v)) goto la;
return i&&!a[i];
}
#define F for
#define P printf
test(char* a, char** b)
{int i;
P("f(\"%s\",[",a);
F(i=0;b[i];++i)
P("\"%s\"%s", b[i], b[i+1]?", ":"");
P("])=%d\n", f(a,b));
}
main()
{char *a1="Hello, world!", *b1[]={"l","He", "o, worl", "d!", 0};//1
char *a2="la lal al ", *b2[]={"la", " l", "al ", 0};//1
char *a3="this is a string", *b3[]={"this should return falsy", 0};//0
char *a4="thi is a string", *b4[]={"this", "i i", " a", " string", 0};//0
char *a5="aaaaa", *b5[]={"aa", 0};//0
char *a6="foo bar foobar", *b6[]={"foo","bar"," ","spam", 0};//1
char *a7="ababab", *b7[]={"a","ba","ab", 0};//1
char *a8="", *b8[]={"This return 0 even if has to return 1", 0};//0
char *a9="ababc", *b9[]={"a","abc", "b", 0};//1
test(a1,b1);test(a2,b2);test(a3,b3);test(a4,b4);test(a5,b5);test(a6,b6);
test(a7,b7);test(a8,b8);test(a9,b9);
}
f("Hello, world!",["l", "He", "o, worl", "d!"])=1
f("la lal al ",["la", " l", "al "])=1
f("this is a string",["this should return falsy"])=0
f("thi is a string",["this", "i i", " a", " string"])=0
f("aaaaa",["aa"])=0
f("foo bar foobar",["foo", "bar", " ", "spam"])=1
f("ababab",["a", "ba", "ab"])=1
f("",["This return 0 even if has to return 1"])=0
f("ababc",["a", "abc", "b"])=1
这将在gcc C ++编译器中进行编译
#include<algorithm>
int f(char*a,char**b){int i,j,k,v,p[256];if(!a||!b||!*b)return -1;for(v=0;v<256&&b[v];++v)p[v]=v;if(v>=256)return -1;la:;for(i=0,j=0;j<v&&a[i];){for(k=0;b[p[j]][k]==a[i]&&a[i];++i,++k);j=b[p[j]][k]?(i-=k),j+1:0;}if(a[i]&&std::next_permutation(p,p+v))goto la;return i&&!a[i];}