Codeforces 1321C Remove Adjacent

题意

给你一个字符串,字符\(s_i\)可以被伤处当且仅当\(s_{i-1}=s_i-1\)\(s_{i+1}=s_i-1\)。问最多能删几个字符。

解题思路

其实,有个很简单的做法就是从\(z\)开始枚举到\(b\),能删就删,因为如果现在枚举到的字符删不掉,之后也不可能能删掉。

但是比赛的时候我突发奇想,搞了个\(O(n^4)\)的区间dp,毕竟\(\left|s\right|\)最大只有100。

大概意思就是区间dp枚举区间,转移就从左往右扫,能删就删。

乱搞也搞过了.jpg

AC代码

#include <bits/stdc++.h>
using namespace std;
  
typedef long long ll;
typedef pair<int,int> pi;
  
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define endl '\n'
  
const double PI=acos(-1.0);
  
namespace IO{
    bool REOF = 1;//为0表示文件结尾
    inline char nc() {
        static char buf[100000], *p1 = buf, *p2 = buf;
        return p1 == p2 && REOF && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? (REOF = 0, EOF) : *p1++;
    }
  
    template<class T>
    inline bool read(T &x) {
        if(!REOF)return false;
        char c = nc();bool f = 0; x = 0;
        while (c<'0' || c>'9')c == '-' && (f = 1), c = nc();
        while (c >= '0'&&c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
        if(f)x=-x;
        return true;
    }
  
    template<typename T, typename... T2>
    inline bool read(T &x, T2 &... rest) {
        if(!REOF)return false;
        read(x);
        return read(rest...);
    }
  
  
    inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')); }
    // inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || c==' '; }
  
    inline bool read_str(char *a) {
        if(!REOF)return false;
        while ((*a = nc()) && need(*a) && REOF)++a; *a = '\0';
        return true;
    }
  
    inline bool read_dbl(double &x){
        if(!REOF)return false;
        bool f = 0; char ch = nc(); x = 0;
        while(ch<'0'||ch>'9')  {f|=(ch=='-');ch=nc();}
        while(ch>='0'&&ch<='9'){x=x*10.0+(ch^48);ch=nc();}
        if(ch == '.') {
            double tmp = 1; ch = nc();
            while(ch>='0'&&ch<='9'){tmp=tmp/10.0;x=x+tmp*(ch^48);ch=nc();}
        }
        if(f)x=-x;
        return true;
    }
  
    template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
  
    template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
        while(*sdbg!=',')cerr<<*sdbg++;
        cerr<<'='<<h<<','<<' '; _dbg(sdbg+1, a...);
    }
     
    template<class T> ostream &operator<<(ostream& os, vector<T> V) {
        os << "["; for (auto vv : V) os << vv << ","; return os << "]";
    }
  
    template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
        return os << "(" << P.st << "," << P.nd << ")";
    }
     
    #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
}
  
// using namespace IO;
const int maxn=2e5+5;
const int maxv=2e5+5;
const int mod=998244353; // 998244353 1e9+7
const int INF=1e9+7; // 1e9+7 0x3f3f3f3f 0x3f3f3f3f3f3f3f3f
const double eps=1e-12;
  
int dx[4]={0,1,0,-1};
//int dx[8]={1,0,-1,1,-1,1,0,-1};
int dy[4]={1,0,-1,0};
//int dy[8]={1,1,1,0,0,-1,-1,-1};
 
// #define ls (x<<1)
// #define rs (x<<1|1)
// #define mid ((l+r)>>1)
// #define lson ls,l,mid
// #define rson rs,mid+1,r
 
// int tot,head[maxn];
// struct Edge{
// 	int v,nxt;
// }e[maxn<<1];
// void init(){
// 	tot=1;
// 	memset(head,0,sizeof(head));
// }
// void addedge(int u,int v){
// 	e[tot].v=v;e[tot].nxt=head[u];
// 	head[u]=tot++;
	
// 	e[tot].v=u;e[tot].nxt=head[v];
// 	head[v]=tot++;
// }
 
  
/**
 * **********     Backlight     **********
 * 仔细读题
 * 注意边界条件
 * 记得注释输入流重定向
 * 没有思路就试试逆向思维
 * 加油,奥利给
 */
 
int n;
string s,dp[105][105];
 
string work(string a,string b){
	string res="",t=a+b;
	for(int i=0;i<sz(t);i++){
		char pre=i-1>=0?t[i-1]:'A';
		char suf=i+1<n?t[i+1]:'A';
		if(t[i]==pre+1 || t[i]==suf+1);
		else res+=t[i];
	}
	return res;
}
 
void solve(){
	cin>>n>>s;
 
	for(int l=0;l<n;l++){
		string tmp="";
		for(int r=l;r<n;r++){
			tmp+=s[r];
			dp[l][r]=tmp;
		}
	}
 
	for(int l=2;l<=n;l++){
		for(int i=0;i<=n-l;i++){
			int j=i+l-1;
			for(int k=i;k<=j-1;k++){
				string tmp=work(dp[i][k],dp[k+1][j]);
				if(sz(tmp)<sz(dp[i][j]))dp[i][j]=tmp;
			}
		}
	}
 
	printf("%d\n",n-sz(dp[0][n-1]));
}
 
int main()
{
    // freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    // int _T; read(_T); for(int _=1;_<=_T;_++)solve();
    // while(read(n))solve();
    solve();
    return 0;
}
posted @ 2020-03-03 20:05  _Backl1ght  阅读(211)  评论(0编辑  收藏  举报