Submission #1274246

#TimeUsernameProblemLanguageResultExecution timeMemory
1274246saleh05Palindromic Partitions (CEOI17_palindromic)C++20
0 / 100
1 ms332 KiB
#include <bits/stdc++.h> using namespace std ; #define int long long #define ll long long #define FAST ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); const int MOD = 1e9 + 7 ; const int INF = 1e18 + 7; const int MOD2 = 1e9 + 9 ; /*------------------------------------------------------------------------------------------------------------------------------*/ int dx[] = { -1 , 1 , 0 , 0 }; int dy[] = { 0 , 0 , -1 , 1 }; void flip(int x , int y , vector<string> &s) { if (s[x][y] == 'b')s[x][y] = 'w'; else s[x][y] = 'b'; for (int i = 0 ; i < 4 ; i++) { int nx = x + dx[i] , ny = y + dy[i]; if (nx >= 0 && nx <4 && ny >= 0 && ny <4) { if (s[nx][ny] == 'b')s[nx][ny] = 'w'; else s[nx][ny] = 'b'; } } } void solve() { // int n ; cin >> n ; string s ; cin >> s ; int n = s.size() ; vector<int> hash(n + 1), pow(n + 1 ); int p = 109; pow[0] = 1 ; for (int i = 1 ; i <= n ; i++) { hash[i] = (hash[i - 1 ] * p + s[i - 1 ]) % MOD ; pow[i] =( pow[i - 1] * p) % MOD ; } auto compare_s = [&](int l , int r ) -> int { return ( hash[r + 1 ] - hash[l] * pow[r - l + 1 ] % MOD + MOD ) % MOD ; }; int l = 0 , r = n - 1 ; int cnt = 0 ; while (l <= r ) { bool found = false ; if (l != r && s[l] == s[r]) { cnt += 2 ; l++, r--; continue ; } for (int len = 1 ; l !=r - len + 1 && len < (r - l + 1) / 2 ; len++) { if (compare_s(l , l + len - 1 ) == compare_s(r - len + 1 , r ) ) { l+= len ; r-= len ; cnt+= 2 ; found = true; break; } } if (!found) { cnt+= 1; break ; } } cout << cnt << '\n'; } int32_t main() { FAST #ifndef ONLINE_JUDGE freopen("saleh.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int t = 1; cin >> t ; while (t--) { solve(); } return 0; }

Compilation message (stderr)

palindromic.cpp: In function 'int32_t main()':
palindromic.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen("saleh.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:71:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     freopen("out.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...