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
|
#include<iostream> #include<sstream> #include<cstring> #include<string> #include<vector> #include<map> #include<set> using namespace std;
map<string, set<unsigned> > word; char lines[1510][85]; unsigned N, M, la[105], ln(1); char tmpword[80], tmpline[88], w1[80], w2[80], w3[80];
void ONLY(bool& flag, const string& on) { auto &t(word[on]); auto p(t.begin()); for (unsigned i(1);i <= N;++i) { while (p != t.end() && *p < la[i - 1]) ++p; if (p != t.end() && *p < la[i]) { if (flag) puts("----------"); else flag = true; while (p != t.end() && *p < la[i]) puts(lines[*p++]); } } }
void NOT(bool& flag, const string& on) { auto &t(word[on]); auto p(t.begin()); for (unsigned i(0);i < N;++i) { while (p != t.end() && *p < la[i]) ++p; if (p == t.end() || *p >= la[i + 1]) { if (flag) puts("----------"); else flag = true; for (auto j(la[i]);j < la[i + 1];++j) puts(lines[j]); } } }
void AND(bool& flag, const string& le, const string& ri) { auto &t1(word[le]), &t2(word[ri]); auto p1(t1.begin()), p2(t2.begin()); for (unsigned i(1);i <= N;++i) { while (p1 != t1.end() && *p1 < la[i - 1]) ++p1; while (p2 != t2.end() && *p2 < la[i - 1]) ++p2; if ((p1 != t1.end() && *p1 < la[i]) && (p2 != t2.end() && *p2 < la[i])) { if (flag) puts("----------"); else flag = true; unsigned re; while ((p1 != t1.end() && *p1 < la[i]) || (p2 != t2.end() && *p2 < la[i])) { if (p1 == t1.end() || *p1 >= la[i]) re = *p2++; else if (p2 == t2.end() || *p2 >= la[i]) re = *p1++; else if (*p2 > *p1) re = *p1++; else if (*p2 < *p1) re = *p2++; else re = *p1++, *p2++; puts(lines[re]); } } } }
void OR(bool& flag, const string& le, const string& ri) { auto p1(word[le].begin()), p2(word[ri].begin()); for (unsigned i(1);i <= N;++i) { while (p1 != word[le].end() && *p1 < la[i - 1]) ++p1; while (p2 != word[ri].end() && *p2 < la[i - 1]) ++p2; if ((p1 != word[le].end() && *p1 < la[i]) || (p2 != word[ri].end() && *p2 < la[i])) { if (flag) puts("----------"); else flag = true; unsigned re; while ((p1 != word[le].end() && *p1 < la[i]) || (p2 != word[ri].end() && *p2 < la[i])) { if (p1 == word[le].end() || *p1 >= la[i]) re = *p2++; else if (p2 == word[ri].end() || *p2 >= la[i]) re = *p1++; else if (*p2 > *p1) re = *p1++; else if (*p2 < *p1) re = *p2++; else re = *p1++, *p2++; puts(lines[re]); } } } }
int main() { #ifdef _XIENAOBAN_ #define gets(T) gets_s(T, 88) freopen("in.txt", "r", stdin); #endif scanf("%d", &N); getchar(); for (unsigned now(0);now < N;++now) { la[now] = ln; while (gets(tmpline) && strcmp(tmpline, "**********")) { strcpy(lines[ln], tmpline); char *pl(tmpline); do { if ((*pl >= 'a' && *pl <= 'z') || (*pl >= 'A' && *pl <= 'Z')) { char *pw(tmpword); while ((*pl >= 'a' && *pl <= 'z') || (*pl >= 'A' && *pl <= 'Z')) { if (*pl >= 'A' && *pl <= 'Z') *pl += 32; *pw++ = *pl++; } *pw = 0; word[tmpword].insert(ln); } } while (*pl++ != 0); ++ln; } } la[N] = ln; scanf("%d", &M); getchar(); int nnnn = 0; while (M--) { gets(tmpline); char *p = tmpline, *pw1 = w1, *pw2 = w2, *pw3 = w3; while (*p && *p != ' ') *pw1++ = *p++; *pw1 = '\0'; if (*p) ++p; while (*p && *p != ' ') *pw2++ = *p++; *pw2 = '\0'; if (*p) ++p; while (*p && *p != ' ') *pw3++ = *p++; *pw3 = '\0'; bool flag(false); if (!*w2) ONLY(flag, w1); else if (*w1 == 'N') NOT(flag, w2); else if (*w2 == 'A') AND(flag, w1, w3); else OR(flag, w1, w3); if (!flag) puts("Sorry, I found nothing."); puts("=========="); } return 0; }
|
v1.5.2