#include<bits/stdc++.h> using namespace std; string w; const bool v[ 26 ] = { 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 }; long long podslow( long long n ) { return max( ( long long )0, ( n * ( n + 1 ) ) / 2 - 2 * n + 1 ); } int main() { ios_base::sync_with_stdio( 0 ); cin >> w; long long res = podslow( w.size() ); long long last = 0; for( long long i = 2; i < w.size(); i++ ){ if( v[ w[ i - 2 ] - 'a' ] == v[ w[ i - 1 ] - 'a' ] && v[ w[ i - 1 ] - 'a' ] == v[ w[ i ] - 'a' ] ){ res -= podslow( i - last ); last = i - 1; } } res -= podslow( w.size() - last ); cout << res << 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 34 35 | #include<bits/stdc++.h> using namespace std; string w; const bool v[ 26 ] = { 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 }; long long podslow( long long n ) { return max( ( long long )0, ( n * ( n + 1 ) ) / 2 - 2 * n + 1 ); } int main() { ios_base::sync_with_stdio( 0 ); cin >> w; long long res = podslow( w.size() ); long long last = 0; for( long long i = 2; i < w.size(); i++ ){ if( v[ w[ i - 2 ] - 'a' ] == v[ w[ i - 1 ] - 'a' ] && v[ w[ i - 1 ] - 'a' ] == v[ w[ i ] - 'a' ] ){ res -= podslow( i - last ); last = i - 1; } } res -= podslow( w.size() - last ); cout << res << endl; return 0; } |