#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#define CHARCONSTANT '\0'
#define MAXWORDS 10000
#define MAXCHARS 100
typedef char word_t[MAXCHARS+1];
int read_store_word(word_t w_list, int lim);
int stage1(word_t all_words[MAXCHARS], int limit);
int overlap_num(char ss[MAXCHARS], char fragment[MAXCHARS], int idx);
char *overlap_words(char *txt, char *pat);
int
read_store_word(char w_list[], int lim) {
int c, nchar=0;
while ((c=getchar()) != EOF && !isalpha(c)) {
}
if (c==EOF) {
return EOF;
}
w_list[nchar++] = c;
while (nchar < lim && (c=getchar())!= EOF && isalpha(c)) {
w_list[nchar++] = c;
}
w_list[nchar] = CHARCONSTANT;
return 0;
}
int
stage1(char all_words[][MAXCHARS+1], int limit) {
word_t frag[MAXCHARS];
char superstr[MAXCHARS];
int i, index, len_ss;
int fir_index;
strcpy(superstr, all_words[0]);
len_ss = strlen(superstr);
for (i=0; i < limit; i++) {
strcpy(frag[i], all_words[i]);
}
for (i=0; i < limit; i++) {
index = strlen(superstr) - strlen(frag[i]);
if (!overlap_words(superstr, frag[i])) {
if (overlap_num(superstr, frag[i], index)) {
fir_index = len_ss-overlap_num(superstr, frag[i], index);
frag[i][0] = toupper(frag[i][0]);
superstr[fir_index] = '\0';
strcat(superstr, frag[i]);
} else {
frag[i][0] = toupper(frag[i][0]);
strcat(superstr, frag[i]);
}
}
printf("%d: frag=%d, slen=%d %s\n", i, i, strlen(superstr), superstr);
}
return 0;
}
int
overlap_num(char superstring[MAXCHARS], char fragment[MAXCHARS], int idx) {
int j, num=0;
for (j=0; j < strlen(superstring) - idx; j++) {
if (tolower(superstring[idx+j]) == tolower(fragment[j])) {
num = num + 1;
}
if (tolower(superstring[idx+j]) != tolower(fragment[j])) {
idx = idx + 1;
num =0;
break;
}
if (num == strlen(superstring) - idx) {
return num;
}
}
return 0;
}
char *overlap_words(char *txt, char *pat) {
int tl=strlen(txt);
int pl=strlen(pat);
int i;
for (i=0; i<=tl-pl; i++) {
if (strncasecmp(txt+i, pat, pl)==0) {
return txt+i;
}
}
return NULL;
}
int
main(int argc, char *argv[]) {
word_t one_word, all_word[MAXWORDS];
int nwords=0, nfrag=0;
while (read_store_word(one_word, MAXWORDS) != EOF) {
nfrag += 1;
if (nwords < MAXWORDS) {
strcpy(all_word[nwords], one_word);
nwords += 1;
}
}
stage1(all_word, nwords);
return 0;
}
版权所有:留学生程序网 2020 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。