分类:
2008-10-13 16:14:22
取3个字符串共有的字母集合有没有好算法?4个呢,5个呢... —— 一个ID叫做[1_]的朋友问!没有仔细考虑算法,凭着直觉,抓起editplus和gcc就开始做,边做边构思,偶似乎又回到了9年前...(多么令人向往的高中生活,555)大约20分钟后,码出了下面这个砖块,偶抛出来了啊,小心……^_^
/*-----------------------------------------------------------*| 题目:取3个字符串共有的字母集合有没有好算法?4个呢,5个呢... | | ------------ | | 备注:题意不是很清晰,我就以可见ascii码为例做做吧。:) | | 先来个不超过8个字串的通用做法:p | | author: Spark Song | | date : 2005-11-29 | \*-----------------------------------------------------------*/ #include#include unsigned char flag_set[ARR_SIZE]; //test str unsigned char str1[] = "hello, world! hi, hoho! I do not know! "; unsigned char str2[] = "every night I have a dream !"; unsigned char str3[] = "Bye bye, I am so sleepy."; void filtrate(unsigned char * str, int order); int main(int argc, char *argv[]) { int i; unsigned char flag; //initialize memset(flag_set, 0, sizeof(flag_set)); //do .... filtrate(str1, 0); filtrate(str2, 1); filtrate(str3, 2); flag = (0x1 | 0x2 | 0x4); //output printf("Now, the char left are: \n"); for (i = CHR_BEGIN; i<=CHR_END; i++) if ((flag_set[i] & flag) == flag) printf("%c", (unsigned char)i); printf("\n"); return 0; } void filtrate(unsigned char * str, int order) { int i, j, k; unsigned char * p; for (p = str; *p != '\0'; p++) flag_set[*p] |= (1<<order); }#define CHR_BEGIN (0x20) #define CHR_END (0x7E) #define ARR_SIZE (CHR_END+1)
Now, the chars left are: Ie这段下意识精神状态下写出的代码最能反映我当前的健康状况了,hoho。等明天睡清醒的时候来检查一下,看看最近参加打羽毛球、乒乓球、游泳等体育活动是不是确实增强了偶的体质!:)