cf1555D. Say No to Palindromes
生活随笔
收集整理的這篇文章主要介紹了
cf1555D. Say No to Palindromes
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
cf1555D. Say No to Palindromes
題意:
給出一個字符串,長度為n,而且都是a,b,c三個字符構成的,然后有m個詢問
每個詢問給出l r,問要想這個區間內任意長度字串都不是回文子串,至少要改多少個字符
題解:
我們思考一下,什么樣的字符是符合要求的:
也就是只要不滿足長度為2和長度為3的字符情況,就是符合要求的。要符合這兩個要求,可以得知要求每三個字符都是不一樣的,一共就三個字符,我們將三個字符全排列。
一個符合要求的字符串一定循環的,且循環節有6種情況:abc,acb,bac,bca,cab,cba
這樣就可以實現三個位置都不相同
我們用前綴和預處理,用前綴和來實現
代碼:
// Problem: D. Say No to Palindromes // Contest: Codeforces - Educational Codeforces Round 112 (Rated for Div. 2) // URL: https://codeforces.com/contest/1555/problem/D // Memory Limit: 256 MB // Time Limit: 2000 ms // Data:2021-08-18 11:42:57 // By Jozky#include <bits/stdc++.h> #include <unordered_map> #define debug(a, b) printf("%s = %d\n", a, b); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; clock_t startTime, endTime; //Fe~Jozky const ll INF_ll= 1e18; const int INF_int= 0x3f3f3f3f; void read(){}; template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar) {x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...); } template <typename T> inline void write(T x) {if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0'); } void rd_test() { #ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin); #endif } void Time_test() { #ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC); #endif } const int maxn= 3e5 + 9; string s1[10]; ll diff[10][maxn]; int main() {//rd_test();ll n, m;read(n, m);string s;cin >> s;s= "#" + s; //為了下標從1開始s1[1]= "#";while (s1[1].size() <= n)s1[1]+= "abc";s1[2]= "#";while (s1[2].size() <= n)s1[2]+= "acb";s1[3]= "#";while (s1[3].size() <= n)s1[3]+= "bac";s1[4]= "#";while (s1[4].size() <= n)s1[4]+= "bca";s1[5]= "#";while (s1[5].size() <= n)s1[5]+= "cab";s1[6]= "#";while (s1[6].size() <= n)s1[6]+= "cba";//Time_test();for (int i= 1; i <= 6; i++) {diff[i][0]= 0;for (int j= 1; j <= n; j++) {if (s1[i][j] == s[j])diff[i][j]= diff[i][j - 1];elsediff[i][j]= diff[i][j - 1] + 1;}}// cout << "--" << endl;while (m--) {int l, r;read(l, r);ll ans= INF_int;for (int i= 1; i <= 6; i++) {ans= min(ans, diff[i][r] - diff[i][l - 1]);}printf("%lld\n", ans);}return 0; }總結
以上是生活随笔為你收集整理的cf1555D. Say No to Palindromes的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cf1555C Coin Rows
- 下一篇: cf1555 E. Boring Seg