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 | #include <stdio.h> #include <string.h> #include <stdbool.h> #include <stdlib.h>
typedef long long LL; typedef struct Mark Mark;
struct Mark { Mark* prev; char* type; };
bool readln(char* str); bool invoke(); void pushItem(char* name, int start, int end); char* popItem(); int indexOfAny(const char* src, int start, int end, char* items); char* indexOfInRange(char* src, int length, const char* that); bool strEquals(char* src, int length, const char* that); bool isSelfClosing(char* src, int start, int end);
char distId[1010] = {'i', 'd', '=', '"'}; char input[1001]; char content[3001]; bool hit = false; Mark* linkedList = NULL;
int main() { readln(distId + 4); int idLength = (int) strlen(distId); distId[idLength] = '"'; distId[idLength + 1] = '\0'; if (!invoke()) printf("duo lao a cjmm"); else if (!hit) printf("duo lao a cjjj"); else { bool isNotBlank = false; for (int i = 0; content[i] != '\0'; ++i) { switch (content[i]) { case ' ': case '\t': case '\n': break; default: isNotBlank = true; goto skipFor; } } skipFor: if (!isNotBlank) printf("QAQ"); else { int start = 0; for (; content[start] != '\0'; ++start) { if (content[start] != '\n') break; } printf("%s", content + start); } } return 0; }
bool invoke() { bool skip = false; char* contentStartFlag = NULL; int contentStartIndex; int contentLength = 0; while (readln(input)) { int length = (int) strlen(input); int i = 0; if (skip) { LL endIndex = (LL) (strstr(input, "-->") - input); if (endIndex < 0) continue; i = (int) (endIndex + 3); } while (i != length) { LL tmp = (LL) (strchr(input + i, '<') - input); if (tmp < 0) break; int startIndex = (int) tmp; if (startIndex + 3 < length && input[startIndex + 1] == '!' && input[startIndex + 2] == '-' && input[startIndex + 3] == '-') { LL endIndex = (LL) (strstr(input + i, "-->") - input); if (endIndex < 0) { skip = true; break; } i = (int) (endIndex + 3); continue; } int endIndex = (int) (strchr(input + startIndex + 1, '>') - input); if (input[startIndex + 1] == '/') { char* oldName = popItem(); if (oldName == NULL) return false; bool isEquals = strEquals(input + startIndex + 2, endIndex - startIndex - 2, oldName); bool isContent = oldName == contentStartFlag; free(oldName); if (!isEquals) return false; if (isContent) { contentStartFlag = NULL; int len = startIndex - contentStartIndex; memcpy(content + contentLength, input + contentStartIndex, sizeof(char) * len); contentLength += len; } } else { int nameEndIndex = indexOfAny(input, startIndex + 1, endIndex + 1, " >"); bool selfClosing = input[endIndex - 1] == '/' || isSelfClosing(input, startIndex + 1, nameEndIndex); if (!selfClosing) pushItem(input, startIndex + 1, nameEndIndex); char* idIndex = indexOfInRange(input + startIndex + 1, endIndex - startIndex - 1, distId); if (idIndex != NULL) { hit = true; if (!selfClosing) { contentStartFlag = linkedList->type; contentStartIndex = endIndex + 1; } } } i = endIndex + 1; } if (contentStartFlag != NULL) { int len = length - contentStartIndex; memcpy(content + contentLength, input + contentStartIndex, sizeof(char) * len); contentLength += len; contentStartIndex = 0; content[contentLength++] = '\n'; } } content[contentLength] = '\0'; return !skip && linkedList == NULL; }
bool readln(char* str) { memset(str, 0, sizeof(char) * 1000); char *flag = gets(str); return flag != NULL; }
int indexOfAny(const char* src, int start, int end, char* items) { for (int i = start; i != end; ++i) { char value = src[i]; char* dist = strchr(items, value); if (dist != NULL) return i; } return -1; }
void pushItem(char* name, int start, int end) { Mark* newItem = malloc(sizeof(Mark)); newItem->prev = linkedList; int length = end - start; newItem->type = malloc(sizeof(char) * (length + 1)); memcpy(newItem->type, name + start, sizeof(char) * length); newItem->type[length] = '\0'; linkedList = newItem; }
char* popItem() { if (linkedList == NULL) return NULL; Mark* oldItem = linkedList; char* oldType = oldItem->type; linkedList = oldItem->prev; free(oldItem); return oldType; }
bool strEquals(char* src, int length, const char* that) { char tmp = src[length]; src[length] = '\0'; bool result = strcmp(src, that) == 0; src[length] = tmp; return result; }
char* indexOfInRange(char* src, int length, const char* that) { char tmp = src[length]; src[length] = '\0'; char* result = strstr(src, that); src[length] = tmp; return result; }
const char SELF_LIST[][11] = {"br", "hr", "img", "input", "link", "meta", "area"};
bool isSelfClosing(char* src, int start, int end) { src = src + start; int length = end - start; for (int i = 0; i != 7; ++i) { if (strEquals(src, length, SELF_LIST[i])) return true; } return false; }
|