user-0 提问: 一个编程练习

回复:
user: user-1
content: 引用 3 楼 user-2 的回复:对于已排好序的数组，没必要这样做吧。。。 擦，居然没看见

user: user-2
content: 引用 2 楼 u012077152 的回复:字典树裸题； 注意在oj提交代码时，别加system("pause") 否则你永远别想AC。 仅供参考(AC)： #include<cstdio> #include<cstdlib> #include<cstring> typedef char State[42]; const int Max_N = 100000; struct trie{ 	int cnt; 	trie *next[32]; }*root, stack[Max_N]; int sz = 0; State rec[110], low[110]; inline char tolow(char ch){ 	return ch >= 'A' && ch <= 'Z' ? ch + 32 : ch; } trie *_calloc(){ 	trie *p = &stack[sz++]; 	p->cnt = 0; 	for (int i = 0; i < 26; i++){ 		p->next[i] = NULL; 	} 	return p; } void insert(trie *x, char *src){ 	char *p = src; 	while (*p != '\0'){ 		if (x->next[*p - 'a'] == NULL) x->next[*p - 'a'] = _calloc(); 		x = x->next[*p - 'a']; 		x->cnt++; 		p++; 	} } int search(trie *x, char *src){ 	char *p = src; 	while (*p != '\0'){ 		if (x == NULL || x->next[*p - 'a'] == NULL) return 0; 		x = x->next[*p - 'a']; 		p++; 	} 	return x->cnt; } int main(){ #ifdef LOCAL 	freopen("in.txt", "r", stdin); 	freopen("out.txt", "w+", stdout); #endif 	State buf, src; 	int i, j, l, m, n = 0, k = 0; 	root = _calloc(); 	while (~scanf("%s", buf)){ 		n = strlen(buf); 		for (i = 0; i < n; i++){ 			low[k][i] = src[i] = tolow(buf[i]); 			rec[k][i] = buf[i]; 		} 		rec[k][i] = low[k++][i] = src[i] = '\0'; 		insert(root, src); 	} 	for (i = 0; i < k; i++){ 		n = strlen(rec[i]); 		for (j = 0; j < n; j++){ 			memset(src, 0, sizeof(src)); 			strncpy(src, low[i], j + 1); 			if (1 == search(root, src) || n == j + 1){ 				printf("%s ", rec[i]); 				m = strlen(src); 				for (l = 0; l < m; l++) printf("%c", rec[i][l]); 				printf("\n"); 				break; 			} 		} 	} 	return 0; }  对于已排好序的数组，没必要这样做吧。。。

user: user-1
content: 字典树裸题； 注意在oj提交代码时，别加system("pause") 否则你永远别想AC。 仅供参考(AC)： #include<cstdio> #include<cstdlib> #include<cstring> typedef char State[42]; const int Max_N = 100000; struct trie{ 	int cnt; 	trie *next[32]; }*root, stack[Max_N]; int sz = 0; State rec[110], low[110]; inline char tolow(char ch){ 	return ch >= 'A' && ch <= 'Z' ? ch + 32 : ch; } trie *_calloc(){ 	trie *p = &stack[sz++]; 	p->cnt = 0; 	for (int i = 0; i < 26; i++){ 		p->next[i] = NULL; 	} 	return p; } void insert(trie *x, char *src){ 	char *p = src; 	while (*p != '\0'){ 		if (x->next[*p - 'a'] == NULL) x->next[*p - 'a'] = _calloc(); 		x = x->next[*p - 'a']; 		x->cnt++; 		p++; 	} } int search(trie *x, char *src){ 	char *p = src; 	while (*p != '\0'){ 		if (x == NULL || x->next[*p - 'a'] == NULL) return 0; 		x = x->next[*p - 'a']; 		p++; 	} 	return x->cnt; } int main(){ #ifdef LOCAL 	freopen("in.txt", "r", stdin); 	freopen("out.txt", "w+", stdout); #endif 	State buf, src; 	int i, j, l, m, n = 0, k = 0; 	root = _calloc(); 	while (~scanf("%s", buf)){ 		n = strlen(buf); 		for (i = 0; i < n; i++){ 			low[k][i] = src[i] = tolow(buf[i]); 			rec[k][i] = buf[i]; 		} 		rec[k][i] = low[k++][i] = src[i] = '\0'; 		insert(root, src); 	} 	for (i = 0; i < k; i++){ 		n = strlen(rec[i]); 		for (j = 0; j < n; j++){ 			memset(src, 0, sizeof(src)); 			strncpy(src, low[i], j + 1); 			if (1 == search(root, src) || n == j + 1){ 				printf("%s ", rec[i]); 				m = strlen(src); 				for (l = 0; l < m; l++) printf("%c", rec[i][l]); 				printf("\n"); 				break; 			} 		} 	} 	return 0; }

user: user-2
content: -  char a[100]={0}; +  char *a = malloc(sizeof(char) * 100); +  memset(a, 0, sizeof(char) * 100);   顺便每个名字最多40个字符，最多100个名字