String (string.cpp)
God TM string DP
Title Description:
Xiao Lin and Liang are making a game. Kobayashi randomly writes out a string that consists only of uppercase letters, and then specifies a non-negative integer m, which illuminates for up to M operations. Each operation is an interchange of two characters adjacent to each other. The goal of the highlight is to make the string with the longest length of the same character the maximum. Your task is to calculate how much this maximum length is.
Sample input:
Abccdcddc
4
Sample output:
4
Ideas:
Set a two-dimensional array of string length x26 size, for each letter in the string, record where it appears, set the corresponding matrix element to 1, the remaining element is 0, and then the column takes precedence over the matrix, and the position where each letter appears is recorded in an array. Calculates the minimum number of moves required to move the same elements between I and J together, and filters out the maximum numbers of consecutive letters under the condition that the minimum number satisfies the constraint. Finally, the maximum number of consecutive letters is compared, and the maximum value can be output.
CODE:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include < algorithm> #define N 26#define M 5050 using namespace std;string S;int is a matrix where the letters are placed 1int a[m][n],m;//a]; F[i] is the maximum number of consecutive int dp (int i, int J, int*a) {if (i = = j) return 0 that holds each letter in the case of satisfying the constraint; 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 < + j + +) {A[i][s[i]-' a '] = 1; }} for (int j = 0; J <; 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, n UM) <= 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 (string.cpp)