有机物碳氢计数
题目
有机物的命名有一套完整的规则。以下是烃的部分命名规则:
- 甲烷到癸烷的名称分别为:
methane
ethane
propane
butane
pentane
hexane
heptane
octane
nonane
decane
。 - 烷基的名称是将对应烷烃词尾的
ane
替换为yl
。同理,烯烃替换成ene
,炔烃替换成yne
,烯基替换成enyl
,炔基替换成ynyl
。例如:乙基ethyl
,乙烯ethene
,乙炔ethyne
,乙烯基ethenyl
,乙炔基ethynyl
。 - 苯,萘,蒽,菲的名称分别为:
benzene
,naphthalene
,anthracene
,phenanthrene
。例如,甲基乙苯methylethylbenzene
,9-甲基菲9-methylphenanthrene
。 - “环”用
cyclo
开头,例如:环丙烷cyclopropane
, 环丁烷cyclobutane
。 - 标识取代基个数的“二 三 四 五 六”分别用
di
,tri
,tetra
,penta
,hexa
开头。例如:1-2-3-三甲苯1,2,3-trimethylbenzene
。
设计程序,输入有机物名称,输出其碳原子和氢原子个数。
例:输入 benzene
,输出 6 6
;输入 2-methylpropane
,输出 4 10
;输入 4,5-dipropynylnaphthalene
,输出 16 12
;输入 2,3,5-trimethyl-4-propyloctane
,输出 14 30
。
保证有机物名称满足上述规则。
答案
解 1:
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct {
char* name;
int carbon_count;
} Prefix;
typedef struct {
char* name;
int carbon_count;
int hydrogen_count;
} Aromatic;
typedef struct {
char* name;
int value;
} Multiplier;
bool ends_with(const char* str, const char* suffix);
bool is_digit_string(const char* str);
int get_prefix_carbon(const char* prefix);
void process_substituents(const char* sub_str, int* carbon_count, int* hydrogen_count, int* num_substitutions);
void count_atoms(const char* organic_name, int* carbon_count, int* hydrogen_count);
Prefix prefixes[] = {
{"meth", 1},
{"eth", 2},
{"prop", 3},
{"but", 4},
{"pent", 5},
{"hex", 6},
{"hept", 7},
{"oct", 8},
{"non", 9},
{"dec", 10},
{NULL, 0}
};
Aromatic aromatics[] = {
{"benzene", 6, 6},
{"naphthalene", 10, 8},
{"anthracene", 14, 10},
{"phenanthrene", 14, 10},
{NULL, 0, 0}
};
Multiplier multipliers[] = {
{"di", 2},
{"tri", 3},
{"tetra", 4},
{"penta", 5},
{"hexa", 6},
{NULL, 0}
};
bool ends_with(const char* str, const char* suffix) {
size_t str_len = strlen(str);
size_t suffix_len = strlen(suffix);
if (str_len < suffix_len) {
return false;
}
return strcmp(str + str_len - suffix_len, suffix) == 0;
}
bool is_digit_string(const char* str) {
while (*str) {
if (!isdigit((unsigned char)*str)) {
return false;
}
str++;
}
return true;
}
int get_prefix_carbon(const char* prefix) {
for (int i = 0; prefixes[i].name != NULL; i++) {
if (strcmp(prefixes[i].name, prefix) == 0) {
return prefixes[i].carbon_count;
}
}
return 0;
}
void process_substituents(const char* sub_str, int* carbon_count, int* hydrogen_count, int* num_substitutions) {
char* temp = malloc(strlen(sub_str) + 1);
if (temp == NULL) {
return;
}
strcpy(temp, sub_str);
char* token = strtok(temp, "-");
while (token != NULL) {
if (!is_digit_string(token) && strchr(token, ',') == NULL) {
int multiplier = 1;
char* sub_name = token;
for (int i = 0; multipliers[i].name != NULL; i++) {
size_t mult_len = strlen(multipliers[i].name);
if (strncmp(token, multipliers[i].name, mult_len) == 0) {
multiplier = multipliers[i].value;
sub_name = token + mult_len;
break;
}
}
const char* suffixes[] = {"yl", "enyl", "ynyl"};
int suffix_type = -1;
for (int s = 0; s < 3; s++) {
if (ends_with(sub_name, suffixes[s])) {
suffix_type = s;
break;
}
}
if (suffix_type != -1) {
size_t suffix_len = strlen(suffixes[suffix_type]);
size_t prefix_len = strlen(sub_name) - suffix_len;
char sub_prefix[20];
if (prefix_len >= sizeof(sub_prefix)) {
token = strtok(NULL, "-");
continue;
}
strncpy(sub_prefix, sub_name, prefix_len);
sub_prefix[prefix_len] = '\0';
int sub_carbons = get_prefix_carbon(sub_prefix);
if (sub_carbons > 0) {
*carbon_count += sub_carbons * multiplier;
if (suffix_type == 0) { // yl
*hydrogen_count += (2 * sub_carbons + 1) * multiplier;
} else if (suffix_type == 1) { // enyl
*hydrogen_count += (2 * sub_carbons - 1) * multiplier;
} else if (suffix_type == 2) { // ynyl
*hydrogen_count += (2 * sub_carbons - 3) * multiplier;
}
*num_substitutions += multiplier;
}
}
}
token = strtok(NULL, "-");
}
free(temp);
}
void count_atoms(const char* organic_name, int* carbon_count, int* hydrogen_count) {
*carbon_count = 0;
*hydrogen_count = 0;
int num_substitutions = 0;
size_t org_len = strlen(organic_name);
// check for aromatic
for (int i = 0; aromatics[i].name != NULL; i++) {
const char* aromatic_name = aromatics[i].name;
size_t aromatic_len = strlen(aromatic_name);
if (org_len >= aromatic_len && ends_with(organic_name, aromatic_name)) {
*carbon_count = aromatics[i].carbon_count;
*hydrogen_count = aromatics[i].hydrogen_count;
size_t sub_len = org_len - aromatic_len;
char* sub_str = malloc(sub_len + 1);
if (sub_str == NULL) {
return;
}
strncpy(sub_str, organic_name, sub_len);
sub_str[sub_len] = '\0';
process_substituents(sub_str, carbon_count, hydrogen_count, &num_substitutions);
free(sub_str);
*hydrogen_count -= num_substitutions;
return;
}
}
// not aromatic, check for non-aromatic
const char* suffixes[] = {"ane", "ene", "yne"};
for (int s = 0; s < 3; s++) {
const char* suffix = suffixes[s];
for (int p = 0; prefixes[p].name != NULL; p++) {
const char* prefix = prefixes[p].name;
// check cyclo first
char base_candidate2[50];
snprintf(base_candidate2, sizeof(base_candidate2), "cyclo%s%s", prefix, suffix);
size_t candidate2_len = strlen(base_candidate2);
if (org_len >= candidate2_len && ends_with(organic_name, base_candidate2)) {
int base_carbons = prefixes[p].carbon_count;
*carbon_count = base_carbons;
if (s == 0) { // ane
*hydrogen_count = 2 * base_carbons;
} else if (s == 1) { // ene
*hydrogen_count = 2 * base_carbons - 2;
} else if (s == 2) { // yne
*hydrogen_count = 2 * base_carbons - 4;
}
size_t sub_len = org_len - candidate2_len;
char* sub_str = malloc(sub_len + 1);
if (sub_str == NULL) {
return;
}
strncpy(sub_str, organic_name, sub_len);
sub_str[sub_len] = '\0';
process_substituents(sub_str, carbon_count, hydrogen_count, &num_substitutions);
free(sub_str);
*hydrogen_count -= num_substitutions;
return;
}
// check non-cyclo
char base_candidate1[50];
snprintf(base_candidate1, sizeof(base_candidate1), "%s%s", prefix, suffix);
size_t candidate1_len = strlen(base_candidate1);
if (org_len >= candidate1_len && ends_with(organic_name, base_candidate1)) {
int base_carbons = prefixes[p].carbon_count;
*carbon_count = base_carbons;
if (s == 0) { // ane
*hydrogen_count = 2 * base_carbons + 2;
} else if (s == 1) { // ene
*hydrogen_count = 2 * base_carbons;
} else if (s == 2) { // yne
*hydrogen_count = 2 * base_carbons - 2;
}
size_t sub_len = org_len - candidate1_len;
char* sub_str = malloc(sub_len + 1);
if (sub_str == NULL) {
return;
}
strncpy(sub_str, organic_name, sub_len);
sub_str[sub_len] = '\0';
process_substituents(sub_str, carbon_count, hydrogen_count, &num_substitutions);
free(sub_str);
*hydrogen_count -= num_substitutions;
return;
}
}
}
// if no base found
*carbon_count = 0;
*hydrogen_count = 0;
}
int main() {
const char* test_cases[] = {
"benzene",
"2-methylpropane",
"methylpropane",
"2,3,5-trimethyl-4-propyloctane",
"dimethylbutane"
};
int num_tests = sizeof(test_cases) / sizeof(test_cases[0]);
for (int i = 0; i < num_tests; i++) {
int carbon, hydrogen;
count_atoms(test_cases[i], &carbon, &hydrogen);
printf("%s: %d %d\n", test_cases[i], carbon, hydrogen);
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
解 2:
c
#include <stdio.h>
#include <string.h>
#include <ctype.h> // For isalpha
int main() {
char s[101]; // Raw input
char filtered_s[101]; // Only letters
int j = 0; // Index for filtered_s
if (scanf("%100s", s) != 1) {
fprintf(stderr, "Error reading input.\n");
return 1;
}
for (int i = 0; s[i] != '\0'; ++i) {
if (isalpha(s[i])) {
filtered_s[j++] = tolower(s[i]); // Store lowercase letters only
}
}
filtered_s[j] = '\0'; // Null-terminate the filtered string
int len = strlen(filtered_s);
if (len == 0) { // Handle case where input had no letters
printf("0 0\n");
return 0;
}
long long c_count = 0;
long long h_count = 0;
int multiplier = 1;
// No is_cyclo flag needed
long long n_ll;
long long h_add_ll;
for (int i = 0; i < len; ++i) {
if (filtered_s[i] == 'c' && i + 5 <= len && strncmp(filtered_s + i, "cyclo", 5) == 0) {
h_count -= 2; // Direct subtraction for cyclo
i += 4; // Advance past 'cycl'. Loop will add 1 for 'o'.
continue; // Continue to the character after "cyclo"
}
if (filtered_s[i] == 'd' && i + 2 <= len && strncmp(filtered_s + i, "di", 2) == 0) {
multiplier = 2; i += 1; continue;
}
if (filtered_s[i] == 't' && i + 3 <= len && strncmp(filtered_s + i, "tri", 3) == 0) {
multiplier = 3; i += 2; continue;
}
if (filtered_s[i] == 't' && i + 5 <= len && strncmp(filtered_s + i, "tetra", 5) == 0) {
multiplier = 4; i += 4; continue;
}
if (filtered_s[i] == 'p' && i + 5 <= len && strncmp(filtered_s + i, "penta", 5) == 0) {
multiplier = 5; i += 4; continue;
}
if (filtered_s[i] == 'h' && i + 4 <= len && strncmp(filtered_s + i, "hexa", 4) == 0) {
// Similar ambiguity with hexane/hexyl etc. Assume multiplier if matched here.
multiplier = 6; i += 3; continue;
}
switch (filtered_s[i]) {
case 'm':
if (i + 6 <= len && strncmp(filtered_s + i, "methyl", 6) == 0) {
n_ll = 1; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier;
multiplier = 1; i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "methane", 7) == 0) {
n_ll = 1; c_count += n_ll; h_count += (2 * n_ll + 2); // Base calculation (non-cyclic)
i += 6;
}
break;
case 'e':
if (i + 7 <= len && strncmp(filtered_s + i, "ethynyl", 7) == 0) {
n_ll = 2; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier;
multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "ethyne", 6) == 0) {
n_ll = 2; c_count += n_ll; h_count += (2 * n_ll - 2); // Base calculation
i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "ethenyl", 7) == 0) {
n_ll = 2; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier;
multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "ethene", 6) == 0) {
n_ll = 2; c_count += n_ll; h_count += (2 * n_ll); // Base calculation
i += 5;
} else if (i + 5 <= len && strncmp(filtered_s + i, "ethyl", 5) == 0) {
n_ll = 2; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier;
multiplier = 1; i += 4;
} else if (i + 6 <= len && strncmp(filtered_s + i, "ethane", 6) == 0) {
n_ll = 2; c_count += n_ll; h_count += (2 * n_ll + 2); // Base calculation
i += 5;
}
break;
case 'p':
if (i + 12 <= len && strncmp(filtered_s + i, "phenanthrene", 12) == 0) {
c_count += 14; h_count += 10; i += 11;
} else if (i + 8 <= len && strncmp(filtered_s + i, "propynyl", 8) == 0) {
n_ll = 3; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier; multiplier = 1; i += 7;
} else if (i + 7 <= len && strncmp(filtered_s + i, "propyne", 7) == 0) {
n_ll = 3; c_count += n_ll; h_count += (2 * n_ll - 2); i += 6;
} else if (i + 8 <= len && strncmp(filtered_s + i, "propenyl", 8) == 0) {
n_ll = 3; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier; multiplier = 1; i += 7;
} else if (i + 7 <= len && strncmp(filtered_s + i, "propene", 7) == 0) {
n_ll = 3; c_count += n_ll; h_count += (2 * n_ll); i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "propyl", 6) == 0) {
n_ll = 3; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier; multiplier = 1; i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "propane", 7) == 0) {
n_ll = 3; c_count += n_ll; h_count += (2 * n_ll + 2); i += 6;
} else if (i + 8 <= len && strncmp(filtered_s + i, "pentynyl", 8) == 0) {
n_ll = 5; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier; multiplier = 1; i += 7;
} else if (i + 7 <= len && strncmp(filtered_s + i, "pentyne", 7) == 0) {
n_ll = 5; c_count += n_ll; h_count += (2 * n_ll - 2); i += 6;
} else if (i + 8 <= len && strncmp(filtered_s + i, "pentenyl", 8) == 0) {
n_ll = 5; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier; multiplier = 1; i += 7;
} else if (i + 7 <= len && strncmp(filtered_s + i, "pentene", 7) == 0) {
n_ll = 5; c_count += n_ll; h_count += (2 * n_ll); i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "pentyl", 6) == 0) {
n_ll = 5; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier; multiplier = 1; i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "pentane", 7) == 0) {
n_ll = 5; c_count += n_ll; h_count += (2 * n_ll + 2); i += 6;
}
break;
case 'b':
if (i + 7 <= len && strncmp(filtered_s + i, "benzene", 7) == 0) {
c_count += 6; h_count += 6; i += 6;
} else if (i + 7 <= len && strncmp(filtered_s + i, "butynyl", 7) == 0) {
n_ll = 4; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "butyne", 6) == 0) {
n_ll = 4; c_count += n_ll; h_count += (2 * n_ll - 2); i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "butenyl", 7) == 0) {
n_ll = 4; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "butene", 6) == 0) {
n_ll = 4; c_count += n_ll; h_count += (2 * n_ll); i += 5;
} else if (i + 5 <= len && strncmp(filtered_s + i, "butyl", 5) == 0) {
n_ll = 4; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier; multiplier = 1; i += 4;
} else if (i + 6 <= len && strncmp(filtered_s + i, "butane", 6) == 0) {
n_ll = 4; c_count += n_ll; h_count += (2 * n_ll + 2); i += 5;
}
break;
case 'h':
if (i + 7 <= len && strncmp(filtered_s + i, "hexynyl", 7) == 0) {
n_ll = 6; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "hexyne", 6) == 0) {
n_ll = 6; c_count += n_ll; h_count += (2 * n_ll - 2); i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "hexenyl", 7) == 0) {
n_ll = 6; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "hexene", 6) == 0) {
n_ll = 6; c_count += n_ll; h_count += (2 * n_ll); i += 5;
} else if (i + 5 <= len && strncmp(filtered_s + i, "hexyl", 5) == 0) {
n_ll = 6; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier; multiplier = 1; i += 4;
} else if (i + 6 <= len && strncmp(filtered_s + i, "hexane", 6) == 0) {
n_ll = 6; c_count += n_ll; h_count += (2 * n_ll + 2); i += 5;
} else if (i + 8 <= len && strncmp(filtered_s + i, "heptynyl", 8) == 0) {
n_ll = 7; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier; multiplier = 1; i += 7;
} else if (i + 7 <= len && strncmp(filtered_s + i, "heptyne", 7) == 0) {
n_ll = 7; c_count += n_ll; h_count += (2 * n_ll - 2); i += 6;
} else if (i + 8 <= len && strncmp(filtered_s + i, "heptenyl", 8) == 0) {
n_ll = 7; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier; multiplier = 1; i += 7;
} else if (i + 7 <= len && strncmp(filtered_s + i, "heptene", 7) == 0) {
n_ll = 7; c_count += n_ll; h_count += (2 * n_ll); i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "heptyl", 6) == 0) {
n_ll = 7; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier; multiplier = 1; i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "heptane", 7) == 0) {
n_ll = 7; c_count += n_ll; h_count += (2 * n_ll + 2); i += 6;
}
break;
case 'o':
if (i + 7 <= len && strncmp(filtered_s + i, "octynyl", 7) == 0) {
n_ll = 8; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "octyne", 6) == 0) {
n_ll = 8; c_count += n_ll; h_count += (2 * n_ll - 2); i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "octenyl", 7) == 0) {
n_ll = 8; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "octene", 6) == 0) {
n_ll = 8; c_count += n_ll; h_count += (2 * n_ll); i += 5;
} else if (i + 5 <= len && strncmp(filtered_s + i, "octyl", 5) == 0) {
n_ll = 8; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier; multiplier = 1; i += 4;
} else if (i + 6 <= len && strncmp(filtered_s + i, "octane", 6) == 0) {
n_ll = 8; c_count += n_ll; h_count += (2 * n_ll + 2); i += 5;
}
break;
case 'n':
if (i + 11 <= len && strncmp(filtered_s + i, "naphthalene", 11) == 0) {
c_count += 10; h_count += 8; i += 10;
} else if (i + 7 <= len && strncmp(filtered_s + i, "nonynyl", 7) == 0) {
n_ll = 9; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "nonyne", 6) == 0) {
n_ll = 9; c_count += n_ll; h_count += (2 * n_ll - 2); i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "nonenyl", 7) == 0) {
n_ll = 9; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "nonene", 6) == 0) {
n_ll = 9; c_count += n_ll; h_count += (2 * n_ll); i += 5;
} else if (i + 5 <= len && strncmp(filtered_s + i, "nonyl", 5) == 0) {
n_ll = 9; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier; multiplier = 1; i += 4;
} else if (i + 6 <= len && strncmp(filtered_s + i, "nonane", 6) == 0) {
n_ll = 9; c_count += n_ll; h_count += (2 * n_ll + 2); i += 5;
}
break;
case 'd':
if (i + 7 <= len && strncmp(filtered_s + i, "decynyl", 7) == 0) {
n_ll = 10; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 4); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "decyne", 6) == 0) {
n_ll = 10; c_count += n_ll; h_count += (2 * n_ll - 2); i += 5;
} else if (i + 7 <= len && strncmp(filtered_s + i, "decenyl", 7) == 0) {
n_ll = 10; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll - 2); h_count += h_add_ll * multiplier; multiplier = 1; i += 6;
} else if (i + 6 <= len && strncmp(filtered_s + i, "decene", 6) == 0) {
n_ll = 10; c_count += n_ll; h_count += (2 * n_ll); i += 5;
} else if (i + 5 <= len && strncmp(filtered_s + i, "decyl", 5) == 0) {
n_ll = 10; c_count += n_ll * multiplier; h_add_ll = (2 * n_ll); h_count += h_add_ll * multiplier; multiplier = 1; i += 4;
} else if (i + 6 <= len && strncmp(filtered_s + i, "decane", 6) == 0) {
n_ll = 10; c_count += n_ll; h_count += (2 * n_ll + 2); i += 5;
}
break;
case 'a':
if (i + 10 <= len && strncmp(filtered_s + i, "anthracene", 10) == 0) {
c_count += 14; h_count += 10; i += 9;
}
break;
case 'c':
break;
default:
break;
} // End switch
} // End for loop
printf("%lld %lld\n", c_count, h_count);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237