字串(string.cpp)

來源:互聯網
上載者:User

標籤:open   cdc   ring   最大連續   遊戲   block   最大值   printf   lin   

字串(string.cpp)

神TM字串DP

題目描述:

小林和亮亮正在做一個遊戲。小林隨意的寫出一個字串,字串僅由大寫字母組成,然後指定一個非負整數m,亮亮可以進行至多m次操作。每次操作為交換相鄰的兩個字元。亮亮的目標是使得操作後的字串出現最長相同的字元的長度最大。你的任務是計算這個最大長度是多少。

範例輸入:

ABCCDCDDC
4

範例輸出:

4

思路:

設定一個字串長度x26大小的二維數組,對於字串中的每個字母,記錄其出現的位置,將對應的矩陣元素設定為1,其餘元素為0,然後按列優先遍曆矩陣,將每個字母出現的位置下標記錄到一個數組裡,計算將i和j之間的相同元素全部移動到一起需要的最小移動次數,在這個最小次數滿足滿足約束條件的前提下,篩選出最大的連續字母的個數。最後比較所有字母的最大連續個數,輸出其中的最大值即可。

CODE:
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#define N 26#define M 5050 using namespace std;string s;int a[M][N],m;//a是矩陣,出現字母的地方都置1int f[M],num[M]; //f[i]是存放各字母在滿足約束的情況下最大的連續數int dp(int i , int j , int*a) {    if(i == j) return 0;    else if(i + 1 == j) return a[j] - a[i] - 1;    else return dp(i + 1 , j - 1 , a) + a[j] - a[i] - (j - i);}inline void init() {    memset(a,0,sizeof(a) );    memset(f,0,sizeof(f) );    memset(num,0,sizeof(num) );}inline void open_judge() {    freopen("string.in","r",stdin);    freopen("string.out","w",stdout);}int main() {    //open_judge();    cin>>s;    scanf("%d",&m);    init();    int len = s.length();    for(int i = 0 ; i < len ; i++) {        for(int j = 0 ; j < 26 ; j++) {            a[i][s[i] - 'A'] = 1;        }    }    for(int j = 0 ; j < 26 ; j++) {        int k = 0;        for(int i = 0 ; i < len;i++) {            if(a[i][j] == 1) num[k++] = i;        }        if(k == 1) f[j] = 1;        else {            int temp = -1;            for(int i = 0 ; i < k ; i++) {                for(int l = i + 1 ; l < k ; l++) {                    if( dp(i , l , num) <= m ) {                        if((l - i) + 1 > temp)                            temp = (l - i) + 1;                    }                }            }            f[j] = temp;        }    }    sort(f , f + N);    printf("%d\n",f[N-1]);    return 0;}

字串(string.cpp)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.