#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll p[ 3 ]{ 131, 157, 173 }; const ll M[ 3 ]{ ( ll )( 1E9 + 7 ), ( ll )( 1E9 + 9 ), ( ll )( 1E9 + 19 ) }; int n; int main() { cin >> n; ll hash[ 3 ][ 2 ]{ { 0 , 0 }, { 0, 0 }, { 0, 0 } }; ll pi[ 3 ]{ 1, 1, 1 }; char c; while( cin >> c ){ for( int i = 0; i < 3; i++ ){ hash[ i ][ 0 ] = ( hash[ i ][ 0 ] + c * pi[ i ] ) % M[ i ]; pi[ i ] = ( pi[ i ] * p[ i ] ) % M[ i ]; hash[ i ][ 1 ] = ( hash[ i ][ 1 ] * p[ i ] + c ) % M[ i ]; } } cout << ( ( hash[ 0 ][ 0 ] == hash[ 0 ][ 1 ] && hash[ 1 ][ 0 ] == hash[ 1 ][ 1 ] && hash[ 2 ][ 0 ] == hash[ 2 ][ 1 ] ) ? "TAK" : "NIE" ) << endl; 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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll p[ 3 ]{ 131, 157, 173 }; const ll M[ 3 ]{ ( ll )( 1E9 + 7 ), ( ll )( 1E9 + 9 ), ( ll )( 1E9 + 19 ) }; int n; int main() { cin >> n; ll hash[ 3 ][ 2 ]{ { 0 , 0 }, { 0, 0 }, { 0, 0 } }; ll pi[ 3 ]{ 1, 1, 1 }; char c; while( cin >> c ){ for( int i = 0; i < 3; i++ ){ hash[ i ][ 0 ] = ( hash[ i ][ 0 ] + c * pi[ i ] ) % M[ i ]; pi[ i ] = ( pi[ i ] * p[ i ] ) % M[ i ]; hash[ i ][ 1 ] = ( hash[ i ][ 1 ] * p[ i ] + c ) % M[ i ]; } } cout << ( ( hash[ 0 ][ 0 ] == hash[ 0 ][ 1 ] && hash[ 1 ][ 0 ] == hash[ 1 ][ 1 ] && hash[ 2 ][ 0 ] == hash[ 2 ][ 1 ] ) ? "TAK" : "NIE" ) << endl; return 0; } |