흠... 잉여력으로 코딩 좀 해봤습니다.
원하시는 게 맞는지는 직접 검증!
자 코드 내려갑니다...
--- cut cut cut -------------------------------
/* wc.c */
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
/******************************************************************************
<입력>
N <- 사전에 있는 word의 수
W1 P1
W1 P1
...
Wn Pn
M <- 문자열의 수
S1
S2
...
Sn
<컴파일 방법>
# gcc -Wall wd.c
<실행 방법>
# ./a.out
5
a 0.01
abc 0.003
bcde 0.23
d 0.2
e 0.9
1
abcde
ok: (a)(bcde)
ok: (abc)(d)(e)
******************************************************************************/
#define MAX_LINE 1024
struct dic {
char *word;
int size;
double prob;
};
static int
word_exists(const char *str, const struct dic *dic, int dic_size)
{
int i;
int len = strlen(str);
for (i = 0; i < dic_size; i++) {
if (len == dic[i].size && !strncmp(dic[i].word, str, dic[i].size)) {
return 1;
}
}
return 0;
}
int
foo(char *buff, int offset, const char *str, const struct dic *dic, int dic_size)
{
int i;
int len = strlen(str);
if (!len) {
buff[offset] = 0;
printf("ok: %s\n", buff);
return 0;
}
for (i = 0; i < dic_size; i++) {
if (len >= dic[i].size && !strncmp(dic[i].word, str, dic[i].size)) {
sprintf(buff + offset, "(%s)", dic[i].word);
offset += (dic[i].size + 2);
foo(buff, offset, str + dic[i].size, dic, dic_size);
offset -= (dic[i].size + 2);
}
}
if (!word_exists(str, dic, dic_size)) {
/*
buff[offset] = 0;
printf("no: %s[%s]\n", buff, str);
*/
}
return -1;
}
#define RN(v) \
({ \
char line[MAX_LINE]; \
RL(line); \
sscanf(line, "%d", &v); \
})
#define RL(line) \
({ \
int size = -1; \
if (fgets(line, sizeof(line), stdin)) {; \
size = strlen(line) - 1; \
line[size] = 0; \
} \
size; \
})
int
main(int argc, char *argv[])
{
char buff[MAX_LINE];
int i;
int dic_size;
RN(dic_size);
struct dic dic[dic_size];
for (i = 0; i < dic_size; i++) {
RL(buff);
int j;
for (j = 0; !isspace(buff[j]); j++)
;
buff[j] = 0;
dic[i].word = strdup(buff);
dic[i].size = strlen(dic[i].word);
dic[i].prob = strtod(buff + (j+1), NULL);
/*printf("%s, %d, %lf\n", dic[i].word, dic[i].size, dic[i].prob);*/
}
int str_num;
RN(str_num);
for (i = 0; i < str_num; i++) {
char str[MAX_LINE];
RL(str);
foo(buff, 0, str, dic, dic_size);
}
return 0;
}
--- cut cut cut -------------------------------