我的代码:
第一题:

程序代码:
//file name: mcstr.c
//feature: get the max common substring from two strings;
//author: wushujie
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LISTARR_MAX 127 //the length of listarr;
typedef struct list_t list_t; //list made of node_t;
typedef struct node_t node_t; //node;
typedef struct maxstr_t maxstr_t; //information of max common string;
struct list_t{
node_t *head; //head point to list;
};
struct node_t{
char *pstr; //point to the proper character of str1;
node_t *next; //point to the next node;
};
struct maxstr_t{
int len;
//len: length of max common string;
char *str; //point to where the common max string from in str1;
};
#define LISTARR_HASH(x) ((x)-'\0') //get the correct position of listarr;
int preproc(char *str, list_t listarr[]);
int getmaxcom(char *str, list_t listarr[], maxstr_t *pmaxstr);
int listarr_init(list_t listarr[]);
int listarr_destroy(list_t listarr[]);
node_t *list_insert(list_t *list, node_t *pnode);
node_t *make_node(char *str);
//=======================================================
int main(int argc, char *argv[])
{
if(argc != 3){
fprintf(stderr, "arguments error!\n"
"usage:\n"
" ./mcstr strings1 strings2\n");
exit(1);
}
list_t listarr[LISTARR_MAX];
maxstr_t maxstr = {0, argv[1]};
listarr_init(listarr);
preproc(argv[1], listarr);
getmaxcom(argv[2], listarr, &maxstr);
listarr_destroy(listarr);
maxstr.str[maxstr.len] = '\0';
fprintf(stdout, "%d \"%s\"\n", maxstr.len, maxstr.str);
return 0;
}
int preproc(char *str, list_t listarr[])
{
while(str[0] != '\0'){
list_insert( &listarr[LISTARR_HASH(str[0])],
make_node(&str[0]));
str++;
}
return 0;
}
int getmaxcom(char *str, list_t listarr[], maxstr_t *pmaxstr)
{
node_t *curnd = NULL;
int laloc = 0,
i = 0;
//traverse_str(str);
while(str[0] != '\0'){
laloc = LISTARR_HASH(str[0]);
//traverse_list(listarr[loc]);
for(curnd=listarr[laloc].head;
curnd != NULL; curnd = curnd->next){
//compare(str, curnd->pstr);
for(i=0; str[i] != '\0' && str[i]==curnd->pstr[i]; i++);
if(i > pmaxstr->len){
pmaxstr->len = i;
pmaxstr->str = &str[0];
}
}
str++;
}
return 0;
}
int listarr_init(list_t listarr[])
{
int i;
for(i=0; i<LISTARR_MAX; i++){
listarr[i].head = NULL;
}
return 0;
}
int listarr_destroy(list_t listarr[])
{
node_t *curnd, *next;
int i;
for(i=0; i<LISTARR_MAX; i++){
curnd = listarr[i].head;
while(curnd != NULL){
next = curnd->next;
free(curnd);
curnd = next;
}
}
return 0;
}
node_t *list_insert(list_t *list, node_t *pnode)
{
pnode->next = list->head;
list->head = pnode;
return pnode;
}
node_t *make_node(char *str)
{
node_t *pnode = malloc(sizeof(node_t));
if(pnode == NULL){
fprintf(stderr, "no memory!");
exit(2);
}
pnode->pstr = str;
pnode->next = NULL;
return pnode;
}
第二题:

程序代码:
//file name:search_same.c
#include <stdio.h>
int search_same(int arr[], int n);
int main()
{
int arr[100];
int i;
for(i=0; i<100; i++){
arr[i] = 100 - i;
}
arr[0] = 0;
arr[34] = 23;
fprintf(stdout, "The same one:%d\n", search_same(arr, 100));
return 0;
}
int search_same(int arr[], int n)
{
int i, value;
for(i=0; i<n; i++){
value = arr[i]%n;
if(arr[value]>=n){
return value;
}else{
arr[value] += n;
}
}
return -1;
}
[
本帖最后由 wsj3000 于 2010-5-2 19:59 编辑 ]