当前位置:网站首页>P6758 [BalticOI2013] Vim

P6758 [BalticOI2013] Vim

2022-06-21 21:52:00 doctorZ_

P6758 [BalticOI2013] Vim

Title Description

Given a length of N N N String S S S,Victor The goal is to make e Delete all , Without deleting other characters .
Victor Use Vim To solve this problem .
However ,Victor Not familiar with Vim, He only knows the three instructions :

  • x: Delete the character at the cursor , The cursor position does not change , You cannot use this command at the last character .
  • h: Move the cursor one space to the left , If the cursor is in the first position , The cursor does not move .
  • f: Followed by a character c c c, It will move the cursor to the first character to its right c c c, c ≠ c\not = c=e.
    Please calculate the e Delete all , Without deleting other characters Key Count .

tips: \text{tips:} tips once f f f The operational cost is 2 2 2, At first it was seen as 1 1 1 Got some false conclusions , to A A A Set up a Hushi pot

Solution

It's not hard to think of a violent d p dp dp, set up f i , j f_{i,j} fi,j Before presentation i i i Location e e e Have been deleted , The current cursor jumps to the position j j j The minimum cost of , Then you can do O ( n 2 v ) O(n^2v) O(n2v) Complexity , among v v v Is the character set size
But this is unacceptable , At this time, a thread head is introduced d p dp dp Things that are
Let's start with the characters e e e Get rid of , Delete one e e e There are two steps , Move from the last character , Delete it , So delete one e e e It takes a price 2 2 2
So the rest of the characters have some edges , If there were two adjacent characters, there would have been e e e Of , This side we call
You have to go through the side , set up n d i nd_i ndi Express i − 1 , i i-1,i i1,i Whether this edge is a required edge

The picture is about whoring from here LOJ2687 BalticOI2013 Vim Thread head DP
This is the general shape when we move , It can be observed that , One side is either by one coming from the back f f f The operation goes through once , Or be f f f Operating a , then h h h Go back , Again f f f In the past 3 3 3 Time , Must pass edge is must be passed 3 3 3 Time of
According to this, we can design d p dp dp, set up f i , j f_{i,j} fi,j Express 1 ∼ i + 1 1\sim i+1 1i+1 All the edges between are passed through , Cover i , i + 1 i,i+1 i,i+1 One operation is f j fj fj The minimum cost of , g i , j , k g_{i,j,k} gi,j,k Indicates to overwrite first i , i + 1 i,i+1 i,i+1 It was used f j fj fj, h h h Come back , And then again f k fk fk To the minimum cost behind
Transfer :
f i , j = min ⁡ { f i , j − 1   ( j ≠ s i & ! n d i )   ,   f i − 1 , s i + 2   ,   g i − 1 , s i , j   ( j ≠ s i )   ,   g i − 1 , s i , s i + 2   } f_{i,j}=\min\{f_{i,j-1}\ (j\not =s_i\& !nd_i)\ ,\ f_{i-1,s_i}+2\ ,\ g_{i-1,s_i,j}\ (j\not =s_i)\ ,\ g_{i-1,s_i,s_i}+2\ \} fi,j=min{ fi,j1 (j=si&!ndi) , fi1,si+2 , gi1,si,j (j=si) , gi1,si,si+2 }
g i , j , k = min ⁡ { f i − 1 , j + 3   ( j ≠ s i )   ,   f i − 1 , s i + 5   ,   g i − 1 , j , k + 1   ( j ≠ s i & k ≠ s i ) , g_{i,j,k}=\min\{f_{i-1,j}+3\ (j\not =s_i)\ ,\ f_{i-1,s_i}+5\ ,\ g_{i-1,j,k}+1\ (j\not =s_i\&k\not =s_i), gi,j,k=min{ fi1,j+3 (j=si) , fi1,si+5 , gi1,j,k+1 (j=si&k=si),
  g i − 1 , s i , k + 3   ( k ≠ s i )   , g i − 1 , j , s i + 3   ( j ≠ s i )   ,   g i − 1 , s i , s i + 5   } \ g_{i-1,s_i,k}+3\ (k\not = s_i)\ ,g_{i-1,j,s_i}+3\ (j\not =s_i)\ ,\ g_{i-1,s_i,s_i}+5\ \}  gi1,si,k+3 (k=si) ,gi1,j,si+3 (j=si) , gi1,si,si+5 }
Then you can do O ( n v 2 ) O(nv^2) O(nv2)
Some details :

  • 1. d p dp dp The answer in does not include e e e At the end of the day 2 2 2
  • 2. You can add a character that does not appear in the string to the end of the string , End of deletion e e e In the future, we regard it as jumping directly to the last character , It is convenient to count the answers

code

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+1000,V=10;
int n,m,nume,p[N+10],nd[N+10],f[N+10][V+1],g[N+10][V+1][V+1];char s[N+10];
int main()
{
    
	scanf("%d %s",&n,s+1);
	for(int i=1;i<=n;i++)
	{
    
		if(s[i]!='e') p[++m]=s[i]-'a',nd[m]=(s[i-1]=='e');
		else nume++;
	}
	for(int i=1;i<=m;i++)
		for(int j=0;j<=V;j++)
		{
    
			for(int k=0;k<=V;k++) g[i][j][k]=3*n+100;
			f[i][j]=3*n+100;
		}
	for(int i=0;i<=V;i++) if(i!='e'-'a') f[1][i]=2;
	for(int i=2;i<=m;i++)
		for(int j=0;j<=V;j++)
		{
    
			if(j!=p[i]&&!nd[i]) f[i][j]=min(f[i][j],f[i-1][j]);
			f[i][j]=min(f[i][j],f[i-1][p[i]]+2);
			if(j!=p[i]) f[i][j]=min(f[i][j],g[i-1][p[i]][j]);
			f[i][j]=min(f[i][j],g[i-1][p[i]][p[i]]+2);
			for(int k=0;k<=V;k++)
			{
    
				if(j!=p[i]) g[i][j][k]=min(g[i][j][k],f[i-1][j]+3);
				g[i][j][k]=min(g[i][j][k],f[i-1][p[i]]+5);
				if(j!=p[i]&&k!=p[i]) g[i][j][k]=min(g[i][j][k],g[i-1][j][k]+1);
				if(j!=p[i]) g[i][j][k]=min(g[i][j][k],g[i-1][j][p[i]]+3);
				if(k!=p[i]) g[i][j][k]=min(g[i][j][k],g[i-1][p[i]][k]+3);
				g[i][j][k]=min(g[i][j][k],g[i-1][p[i]][p[i]]+5);
			}
		}
	printf("%d",f[m][10]+2*nume-2);
	return 0;
}
原网站

版权声明
本文为[doctorZ_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211958572280.html