日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cf1555D. Say No to Palindromes

發布時間:2023/12/3 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cf1555D. Say No to Palindromes 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

cf1555D. Say No to Palindromes

題意:

給出一個字符串,長度為n,而且都是a,b,c三個字符構成的,然后有m個詢問
每個詢問給出l r,問要想這個區間內任意長度字串都不是回文子串,至少要改多少個字符

題解:

我們思考一下,什么樣的字符是符合要求的:

  • 長度為2,相鄰兩個字符不能相同
  • 長度為3,間隔為1的字符不能一樣
  • 長度為4,只要不滿足長度為2和長度為3的情況,一定不是回文串
  • 也就是只要不滿足長度為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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。