manacher || 擴充套件kmp -- Best Reward HDU - 3613

多行不譯必自閉發表於2020-12-11

Best Reward HDU - 3613

題意:
T次詢問,每次給你一個字串,字串僅由26個小寫字母組成,每個小寫字母都有一個屬性值。要你計算字串的最大貢獻。把字串分成前後兩個子串,若子串迴文串則該子串貢獻為字母的屬性和,否則貢獻為0。

擴充套件kmp思路:
用擴充套件kmp求出每個迴文字首和迴文字尾並標記。在遍歷劃分子串求最大值。

擴充套件kmp思路程式碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 1 << 30;
char s[maxn], t[maxn];
int alp[50], nxt[maxn], sum[maxn];
int extend1[maxn], extend2[maxn];
bool pre[maxn], tai[maxn];
void get_N(char *ch, int len){
	int p, j = 0;
	nxt[0] = len;
	while( j + 1 < len && ch[1 + j] == ch[j]) j++; nxt[1] = j;
	p = 1;
	for(int i = 2; i < len; i++){
		if(nxt[i - p] + i < nxt[p] + p) 
		   nxt[i] = nxt[i - p];
		else{
			j = nxt[p] + p - i;
			if(j < 0) j = 0;
			while(i + j < len && ch[i + j] == ch[j]) j++; nxt[i] = j;
			p = i;
		}
	}	
}

void exkmp(char* str1, char* str2, int* ext) { 
	int len = strlen(str1);
	get_N(str2, len);
	int j = 0, p; 
	while(j < len && str1[j] == str2[j]) j++; ext[0] = j;
	p = 0;
	for(int i = 1; i < len; i++){
  		if(nxt[i - p] + i < ext[p] + p) 
		   ext[i] = nxt[i - p];
		else {                                                                                           
			j = ext[p] + p - i; 
			if(j < 0) j = 0;
			while(i + j < len && str1[i + j] == str2[j]) j++; ext[i] = j;
			p = i;
		}
	}		
}

int main(){
	int T;
	scanf("%d", &T);
	while(T--){
		for(int i = 0; i < 26; i++)
		   scanf("%d", &alp[i]);
		scanf("%s", s);
		int len = strlen(s);
		for(int i = 0; i < len; i++){
			if(i > 0) sum[i] = sum[i - 1] + alp[s[i] - 'a'];
			else sum[i] = alp[s[i] - 'a'];
			t[i] = s[len - 1 - i];
		}
		
	
		exkmp(t, s, extend1);
		exkmp(s, t, extend2);
	

		
		int ans = -inf;		
		for(int i = 1; i < len; i++){
			int cnt = 0;
			if(i + extend1[i] == len) cnt += sum[len - 1 - i];
			int pos = len - i;
			if(pos + extend2[pos] == len) cnt += sum[len - 1] - sum[pos - 1]; 
			ans = max(ans, cnt);
		}
		
		printf("%d\n", ans);
	}
}

manacher思路:
用manacher處理出所有迴文子串,遍歷迴文子串把字首迴文串和字尾迴文串標記出來。遍歷劃分子串求最大值。

manacher思路程式碼:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 1 << 30;
char s[maxn], t[maxn * 2];
int lengh[maxn * 2], sum[maxn * 2], alp[50];
bool pre[maxn], tai[maxn];

void manacher(){
	int len = strlen(s);
	int cot = 0;
	t[cot++] = '0';
	for(int i = 0; i < len; i++){
		t[cot++] = '#';
		t[cot++] = s[i];		
	}
	t[cot++] = '#'; 
	t[cot] = '*';
	
	
    lengh[0] = 0;
	int mid = 0;
	for(int i = 1; i < len * 2 + 2; i++){
		if(lengh[mid] + mid > i) lengh[i] = min(lengh[mid] + mid - i, lengh[mid * 2 - i]);
		else lengh[i] = 1;
		while(t[i + lengh[i]] == t[i - lengh[i]]) lengh[i]++;
		
		if(i + lengh[i] > mid + lengh[mid]) mid = i; 
	}

	
    memset(pre, 0, sizeof(pre));
	memset(tai, 0, sizeof(tai));
	for(int i = 2; i < len * 2 + 2; i++){
		if(i - lengh[i] == 0) pre[lengh[i] - 2] = true;
		if(i + lengh[i] == len * 2 + 2) tai[len - lengh[i] + 1] = true;
	} 


	int ans = -inf;
	for(int i = 0; i < len - 1; i++){
		int cnt = 0;
		if(pre[i]) cnt += sum[i];
		if(tai[i + 1]) cnt += sum[len - 1] - sum[i];
		ans = max(ans, cnt);
	}
	
	printf("%d\n", ans);
}

int main(){
     int T;
     scanf("%d", &T);
     while(T--){
     	for(int i = 0; i < 26; i++)
     	   scanf("%d", &alp[i]);
     	scanf("%s", s);
     	int len = strlen(s);
     	for(int i = 0; i < len; i++){
     		if(i > 0) sum[i] = sum[i - 1] + alp[s[i] - 'a'];
     		else sum[i] = alp[s[i] - 'a'];
		 }
		 
 		manacher();
				
	 }


}

相關文章