C - -74 71 64
这没有超越Peter Taylor的解决方案,但我认为这很有趣(而且,这是一个完整的工作程序,而不仅仅是一个功能)
main(int c,char**v){for(;*v[1]!=0;++v[1])v[2]+=*v[1]==*v[2];return*v[2];}
main(int c,char**v){for(;*v[1];++v[1])v[2]+=*v[1]==*v[2];return*v[2];}
main(c,v)char**v;{while(*v[1])v[2]+=*v[1]++==*v[2];return*v[2];}
和无高尔夫球场:
main(int argc, char** argv){
char * input = argv[1];
char * test = argv[2];
// advance through the input string. Each time the current input
// character is equal to the current test character, increment
// the position in the test string.
for(; *input!='\0'; ++input) test += *input == *test;
// return the character that we got to in the test string.
// if it is '\0' then we got to the end of the test string which
// means that it is a subsequence, and the 0 (EXIT_SUCCESS) value is returned
// otherwise something non-zero is returned, indicating failure.
return *test;
}
要测试它,您可以执行以下操作:
./is_subsequence banana anna && echo "yes" || echo "nope"
# yes
./is_subsequence banana foobar && echo "yes" || echo "nope"
# nope