#include <iostream> #include <fstream> #include <vector> void read_to_table(std::istream & str, long n) { std::string buf; buf.reserve(n); str >> buf; if( buf.empty() ) { std::cout << "NIE" << std::endl; return; } long a = 0; long b = buf.size() - 1; while( a < b ) { if( buf[a] != buf [b] ) { std::cout << "NIE" << std::endl; return; } a += 1; b -= 1; } std::cout << "TAK" << std::endl; } void read_all(std::istream & str) { std::vector<long> counter; std::vector<unsigned long> pos; size_t size = 'z' - 'a' + 1; counter.resize(size); pos.resize(size); for(size_t i=0 ; i<size ; ++i) { counter[i] = 0; pos[i] = 0; } int c; unsigned long p = 0; char cc; do { str >> cc; } while( !str.eof() && !(cc>='a' && cc<='z') ); c = (int)cc; do { if( c>='a' && c<='z' ) { c = c - 'a'; counter[c] += 1; pos[c] += p; p += 1; } } while( (c=str.get()) != -1 ); int len = 0; for(size_t i = 0 ; i<counter.size() ; ++i) { if( (counter[i] & 0x01) != 0 ) len += 1; } if( len > 1 ) { std::cout << "NIE" << std::endl; return; } for(size_t i = 0 ; i<pos.size() ; ++i) { if( counter[i] > 0 && (counter[i] & 0x01) == 0 ) { //pos[i] + counter[i] / 2 == p * counter[i] / 2; if( pos[i] / (counter[i] / 2) + 1 != p ) { std::cout << "NIE" << std::endl; return; } } } std::cout << "TAK" << std::endl; } void read(std::istream & str) { long n; str >> n; if( n != 0 && n < 4000000 ) read_to_table(str, n); else read_all(str); } int main() { // std::fstream file("input.txt"); // // if( !file) // { // std::cout << "nie moge otworzyc pliku wejsciowego" << std::endl; // return 1; // } // // read(file); read(std::cin); }
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 | #include <iostream> #include <fstream> #include <vector> void read_to_table(std::istream & str, long n) { std::string buf; buf.reserve(n); str >> buf; if( buf.empty() ) { std::cout << "NIE" << std::endl; return; } long a = 0; long b = buf.size() - 1; while( a < b ) { if( buf[a] != buf [b] ) { std::cout << "NIE" << std::endl; return; } a += 1; b -= 1; } std::cout << "TAK" << std::endl; } void read_all(std::istream & str) { std::vector<long> counter; std::vector<unsigned long> pos; size_t size = 'z' - 'a' + 1; counter.resize(size); pos.resize(size); for(size_t i=0 ; i<size ; ++i) { counter[i] = 0; pos[i] = 0; } int c; unsigned long p = 0; char cc; do { str >> cc; } while( !str.eof() && !(cc>='a' && cc<='z') ); c = (int)cc; do { if( c>='a' && c<='z' ) { c = c - 'a'; counter[c] += 1; pos[c] += p; p += 1; } } while( (c=str.get()) != -1 ); int len = 0; for(size_t i = 0 ; i<counter.size() ; ++i) { if( (counter[i] & 0x01) != 0 ) len += 1; } if( len > 1 ) { std::cout << "NIE" << std::endl; return; } for(size_t i = 0 ; i<pos.size() ; ++i) { if( counter[i] > 0 && (counter[i] & 0x01) == 0 ) { //pos[i] + counter[i] / 2 == p * counter[i] / 2; if( pos[i] / (counter[i] / 2) + 1 != p ) { std::cout << "NIE" << std::endl; return; } } } std::cout << "TAK" << std::endl; } void read(std::istream & str) { long n; str >> n; if( n != 0 && n < 4000000 ) read_to_table(str, n); else read_all(str); } int main() { // std::fstream file("input.txt"); // // if( !file) // { // std::cout << "nie moge otworzyc pliku wejsciowego" << std::endl; // return 1; // } // // read(file); read(std::cin); } |