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%かどうかはわかりませんが、これは品質が悪い可能性があります。NULLで終わるアルゴリズムは簡単です。リスト内のすべての文字列が引数「a」文字列に適合する場合、線形性が試行するアルゴリズムが1つあります。
ungolf it、テストコードと結果はこちら
#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];}