#include <cmath> #include <iostream> #include <cstdio> #include <string> using namespace std; #define LONG long long //#define LONG __int64 char* c1 = 0; char* c2 = 0; char* indata = 0; long lCount = 0L; long lSwitch = 1000L; bool ParseShort(bool bKnown) { if(bKnown) { indata = new char[lCount]; cin.read(indata, lCount); } c1 = indata; c2 = c1; c2 += cin.gcount() - 1; if(*c2 < 'a' || *c2 > 'z') --c2; while(c2 >= c1) { if(*c2 != *c1) return false; --c2; ++c1; } return true; } bool ParseLong(bool bKnown) { LONG tall[26]; LONG teven[26]; LONG todd[26]; LONG tdistall[26]; LONG tdisteven[26]; LONG tdistodd[26]; for(long n = 0; n < 26; ++n) { tall[n] = 0LL; teven[n] = 0LL; todd[n] = 0LL; tdistall[n] = 0LL; tdisteven[n] = 0LL; tdistodd[n] = 0LL; } int idx = 0; int lastchar = 0; long lldist = 0LL; bool bEven = true; long lDoubleCheck = -1L; if(bKnown && ((lCount & 1L) == 0L)) lDoubleCheck = lCount / 2L; if(bKnown) { indata = new char[lSwitch]; cin.read(indata, lSwitch); } int rc = cin.gcount(); do { c1 = indata; while(rc && *c1 > 20) { idx = *c1 - 'a'; if(lDoubleCheck != lldist) { ++tall[idx]; tdistall[idx] += lldist; if(bEven) { ++teven[idx]; tdisteven[idx] += lldist; bEven = false; } else { ++todd[idx]; tdistodd[idx] += lldist; bEven = true; } ++lldist; } else if(lastchar != idx) return false; else lDoubleCheck = -1L; lastchar = idx; ++c1; --rc; } cin.read(indata, lSwitch); rc = cin.gcount(); }while (rc > 0); --lldist; LONG llcenter = lldist; //doubled!!!! long nOdds = 0L; long double ldiffodd = 0.0L; long double ldiffeven = 0.0L; long double ldepsilon = 0.0L; long double ldd = 0.0L; for(idx = 0; idx < 26; ++idx) { if(tall[idx] & 1LL) ++nOdds; } if(bEven && (nOdds > 0L)) return false; if(!bEven && (nOdds > 1L)) return false; for(idx = 0; idx < 26; ++idx) { if(tall[idx] > 0LL) { ldd = (1.0L * llcenter) - ((2.0L * tdistall[idx]) / (1.0L * tall[idx])); if(fabs(ldd) > ldepsilon)// (((tdistall[idx] * 2LL) / tall[idx]) != llcenter) return false; if(todd[idx] > 0LL && teven[idx] > 0LL) { ldiffodd = (1.0L * llcenter) - ((2.0L * tdistodd[idx]) / (1.0L * todd[idx]));// - 1.0L * llcenter; ldiffeven = (1.0L * llcenter) - ((2.0L * tdisteven[idx]) / (1.0L * teven[idx]));// - 1.0L * llcenter; ldd = fabs(ldiffeven + ldiffodd); if(ldd > ldepsilon) return false; } } } return true; } bool ParseUnknown() { bool bRes = false; lCount = lSwitch; indata = new char[lSwitch]; cin.read(indata, lSwitch); if(cin.gcount() < lSwitch) bRes = ParseShort(false); else bRes = ParseLong(false); return bRes; } bool ParseMedium() { long lSize = lCount / 2L; indata = new char[lSize]; cin.read(indata, lSize); c1 = indata; c2 = c1; c2 += cin.gcount() - 1; if(lCount & 1L) getchar(); while(c2 >= c1) { if(*c2 != getchar()) return false; --c2; } return true; } int main(int argc, char* argv[]) { bool bResult = false; scanf("%ld\n", &lCount); if(lCount == 0L) bResult = ParseUnknown(); else if(lCount <= lSwitch) bResult = ParseShort(true); else if(lCount <= 2 * lSwitch) bResult = ParseMedium(); else bResult = ParseLong(true); if(bResult) printf("TAK\n"); else printf("NIE\n"); 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 | #include <cmath> #include <iostream> #include <cstdio> #include <string> using namespace std; #define LONG long long //#define LONG __int64 char* c1 = 0; char* c2 = 0; char* indata = 0; long lCount = 0L; long lSwitch = 1000L; bool ParseShort(bool bKnown) { if(bKnown) { indata = new char[lCount]; cin.read(indata, lCount); } c1 = indata; c2 = c1; c2 += cin.gcount() - 1; if(*c2 < 'a' || *c2 > 'z') --c2; while(c2 >= c1) { if(*c2 != *c1) return false; --c2; ++c1; } return true; } bool ParseLong(bool bKnown) { LONG tall[26]; LONG teven[26]; LONG todd[26]; LONG tdistall[26]; LONG tdisteven[26]; LONG tdistodd[26]; for(long n = 0; n < 26; ++n) { tall[n] = 0LL; teven[n] = 0LL; todd[n] = 0LL; tdistall[n] = 0LL; tdisteven[n] = 0LL; tdistodd[n] = 0LL; } int idx = 0; int lastchar = 0; long lldist = 0LL; bool bEven = true; long lDoubleCheck = -1L; if(bKnown && ((lCount & 1L) == 0L)) lDoubleCheck = lCount / 2L; if(bKnown) { indata = new char[lSwitch]; cin.read(indata, lSwitch); } int rc = cin.gcount(); do { c1 = indata; while(rc && *c1 > 20) { idx = *c1 - 'a'; if(lDoubleCheck != lldist) { ++tall[idx]; tdistall[idx] += lldist; if(bEven) { ++teven[idx]; tdisteven[idx] += lldist; bEven = false; } else { ++todd[idx]; tdistodd[idx] += lldist; bEven = true; } ++lldist; } else if(lastchar != idx) return false; else lDoubleCheck = -1L; lastchar = idx; ++c1; --rc; } cin.read(indata, lSwitch); rc = cin.gcount(); }while (rc > 0); --lldist; LONG llcenter = lldist; //doubled!!!! long nOdds = 0L; long double ldiffodd = 0.0L; long double ldiffeven = 0.0L; long double ldepsilon = 0.0L; long double ldd = 0.0L; for(idx = 0; idx < 26; ++idx) { if(tall[idx] & 1LL) ++nOdds; } if(bEven && (nOdds > 0L)) return false; if(!bEven && (nOdds > 1L)) return false; for(idx = 0; idx < 26; ++idx) { if(tall[idx] > 0LL) { ldd = (1.0L * llcenter) - ((2.0L * tdistall[idx]) / (1.0L * tall[idx])); if(fabs(ldd) > ldepsilon)// (((tdistall[idx] * 2LL) / tall[idx]) != llcenter) return false; if(todd[idx] > 0LL && teven[idx] > 0LL) { ldiffodd = (1.0L * llcenter) - ((2.0L * tdistodd[idx]) / (1.0L * todd[idx]));// - 1.0L * llcenter; ldiffeven = (1.0L * llcenter) - ((2.0L * tdisteven[idx]) / (1.0L * teven[idx]));// - 1.0L * llcenter; ldd = fabs(ldiffeven + ldiffodd); if(ldd > ldepsilon) return false; } } } return true; } bool ParseUnknown() { bool bRes = false; lCount = lSwitch; indata = new char[lSwitch]; cin.read(indata, lSwitch); if(cin.gcount() < lSwitch) bRes = ParseShort(false); else bRes = ParseLong(false); return bRes; } bool ParseMedium() { long lSize = lCount / 2L; indata = new char[lSize]; cin.read(indata, lSize); c1 = indata; c2 = c1; c2 += cin.gcount() - 1; if(lCount & 1L) getchar(); while(c2 >= c1) { if(*c2 != getchar()) return false; --c2; } return true; } int main(int argc, char* argv[]) { bool bResult = false; scanf("%ld\n", &lCount); if(lCount == 0L) bResult = ParseUnknown(); else if(lCount <= lSwitch) bResult = ParseShort(true); else if(lCount <= 2 * lSwitch) bResult = ParseMedium(); else bResult = ParseLong(true); if(bResult) printf("TAK\n"); else printf("NIE\n"); return 0; } |