跳到正文

题解

不想着去证明写什么题解 || solution - AT_tdpc_iwi

bsdsdb约 25 分钟
原文
文章信息与版本

文章信息

发布
2025年4月5日
收录
2025年12月12日
更新
2026年10月28日
篇幅
约 25 分钟

版本

  1. v1当前

写题解总喜欢不证明,不证明就敢写,坏习惯。/kk

读了其它几篇题解,发现没有证明结论的……到底也没怎么搞明白那么做为什么对,于是写一下我的思路,证明应该严谨所以不用意会。


题意:ss 是个由 iw 组成的字符串。连续的 iwi 可以删除,然后左右拼在一起。求最多可能的操作次数。∣s∣≤300|s|\le300。

考虑区间 dp,设 dpl,r\mathrm{dp}_{l,r} 表示 s[l,r]s_{[l,r]} 能删掉多少个字符。显然 dpl,r\mathrm{dp}_{l,r} 至少是 max⁡k∈[l,r)(dpl,k+dpk+1,r)\max_{k\in[l,r)}(\mathrm{dp}_{l,k}+\mathrm{dp}_{k+1,r})。因为可以选个分割点,删左边,删右边。而且对于 s[l,r]s_{[l,r]} 删不完的情况,这就是答案。证:考虑一种删最多的删法,设最后留下的最右边的字符的索引为 xx,那么答案应当是 dpl,r=dpl,x−1+dpx,r\mathrm{dp}_{l,r}=\mathrm{dp}_{l,x-1}+\mathrm{dp}_{x,r}。当 x=lx=l 时答案还等于 dpl,x+dpx+1,r\mathrm{dp}_{l,x}+\mathrm{dp}_{x+1,r} ,在 k=xk=x 时计算过了;否则我们在 k=x−1k=x-1 的时候也的确考虑了这种情况。

还有一种情况没考虑:s[l,r]s_{[l,r]} 能删完,且没有分割点使得删左边删右边能删完的情况。首先他要能删完的话肯定有 sl=sr=is_l=s_r=\texttt i。剩下那个 w 哪来的?肯定是 dpl+1,r−1\mathrm{dp}_{l+1,r-1} 唯一剩下的呗。也就是 (sl=sr=i)∧(Ir−1−Il=2(Wr−1−Wl−1))∧(3∣r−l+1)∧dpl+1,r−1=r−l−2(s_l=s_r=\texttt i)\land(I_{r - 1} - I_l=2(W_{r-1}-W_l-1))\land(3\mid r-l+1)\land\mathrm{dp}_{l+1,r-1}=r-l-2 时 dpl,r=r−l+1\mathrm{dp}_{l,r}=r-l+1,其中 IxI_x 代表前缀 i 的数量,WW 同理。最终答案就是 dp1,n3\frac{\mathrm{dp}_{1,n}}3。

C++
#include <bits/stdc++.h>
#define CODEFORCES
#ifdef CODEFORCES 
#pragma GCC optimize(1,2,3,"Ofast","inline","unroll-loops")
#endif
using namespace std; 
namespace heead {
	// types
	using i32 = int32_t;
	using i64 = int64_t;
	using i128 = __int128;
	using u32 = unsigned int;
	using u64 = unsigned long long;
	using f32 = float;
	using f64 = double;
	using f128 = __float128;
	using ll = i64;
	using ull = u64;
	using ldb = long double;
	#define umap unordered_map
	#define uset unordered_set
	#define prque priority_queue
	#define pi32 pair<i32, i32>
	#define pi64 pair<i64, i64>
	#define pll pair<ll, ll>
	// functions
	#define emp emplace
	#define empb emplace_back
	#define empf emplace_front
	#define mkp make_pair
	mt19937_64 rd(chrono::high_resolution_clock().now().time_since_epoch().count());
	// constants
	#define csep constexpr
	csep i64 inf = 0x3f3f3f3f3f3f3f3fll;
	csep f64 eps = 1e-7;
	csep f64 PI = 3.14159265358979323846264338327950288419716939937510582097;
	// non-std type io
	istream& operator>>(istream& in, i128& x) {
		string input;
		in >> input;
		x = 0;
		for (char c : input) {
			if (c == '-') {
				continue;
			}
			x = (x << 3) + (x << 1) + (c & 15);
		}
		if (input[0] == '-') {
			x *= -1;
		}
		return in;
	}
	ostream& operator<<(ostream& out, i128 x) {
		stack<char> output;
		if (x < 0) {
			out << '-';
			x *= -1;
		}
		if (!x) {
			out << '0';
			return out;
		}
		while (x) {
			output.emp((x % 10) | 48);
			x /= 10;
		}
		while (!output.empty()) {
			out << output.top();
			output.pop();
		}
		return out;
	}
	istream& operator>>(istream& in, f128& x) {
		string input;
		in >> input;
		f128 mul = 1;
		i64 id = (input.find('.') == string::npos ? input.length() : input.find('.')) - (input[0] == '-') - 1;
		for (ll i = 1; i <= id; ++i) {
			mul *= 10;
		}
		x = 0;
		for (char c : input) {
			if (c == '.' || c == '-') {
				continue;
			}
			x += mul * (c & 15);
			mul /= 10;
		}
		if (input[0] == '-') {
			x *= -1;
		}
		return in;
	}
	csep i64 F128ACC = 12;
	ostream& operator<<(ostream& out, f128 x) {
		// negative case
		if (x < 0) {
			out << '-';
			x *= -1;
		}
		// integer part
		out << i128(x);
		x -= i128(x);
		// fractional part
		for (ll i = 1; i <= F128ACC; ++i) {
			x *= 10;
		}
		if (!x) {
			return out;
		}
		out << '.';
		// prefix 0
		i128 y = x;
		i128 _y = y;
		for (ll i = 1; i <= F128ACC; ++i) {
			if (!_y) {
				out << '0';
			}
			_y /= 10;
		}
		// suffix 0
		while (y && y % 10 == 0) {
			y /= 10;
		}
		// out
		out << i128(y);
		return out;
	}
}
using namespace heead;

namespace modint {
	#define tpl template<ll P, typename T = i64>
	#define targ P, T
	#define tmint mint<targ>
	#define multp i64
	tpl struct mint {
		T val;
		static i64 dynmod;
		static i64 mod() {
			return P > 0 ? P : dynmod;
		}
		static void setmod(ll x) {
			dynmod = x;
		}
		mint(multp x = 0) {
			while (x < 0) {
				if (x < -mod()) {
					x %= mod();
				}
				x += mod();
			}
			if (mod() <= x && x < mod() * 2) {
				x -= mod();
			}
			if (x < mod()) {
				val = x;
			} else {
				val = x % mod();
			}
			
		}
	};
	template<> i64 mint<0>::dynmod = 1;
	template<> i64 mint<-1>::dynmod = 1;
	template<> i64 mint<-2>::dynmod = 1;
	#define relop(o, tp) tpl tp operator o (tmint x, tmint y) {return tp((multp(x.val) o y.val));}\
	tpl tp operator o (tmint x, ll y) {return x o tmint(y);}\
	tpl tp operator o (ll x, tmint y) {return tmint(x) o y;}
	relop(+, tmint) relop(-, tmint) relop(*, tmint) relop(==, bool) relop(!=, bool)
	#undef relop
	tpl tmint qpow(tmint x, ll y) {
		tmint ret = 1;
		for (; y; y >>= 1) {
			if (y & 1) {
				ret *= x;
			}
			x *= x;
		}
		return ret;
	}
//	#define LINEAR_INV
	#ifdef LINEAR_INV
	csep i64 LI_MAX = 7e5;
	csep i64 INVMOD = 998244353;
	mint<INVMOD> linv[LI_MAX];
	void linear_inv_init() {
		assert(LI_MAX != 1);
		assert(INVMOD != 1);
		linv[1] = 1;
		for (ll i = 2; i < LI_MAX; ++i) {
			i64 k = LI_MAX / i, r = LI_MAX % i;
			linv[i] = mint<INVMOD>(-k) * linv[r];
		}
	}
	tpl tmint operator/(tmint x, tmint y) {
		assert(y.val);
		assert(y.val < LI_MAX);
		return tmint(x * linv[y.val]);
	}
	#else
	tpl tmint operator/(tmint x, tmint y) {
		assert(y.val);
		return tmint(x * qpow(y, tmint::mod() - 2));
	}
	#endif
	tpl tmint operator/(tmint x, i64 y) {
		return x / tmint(y);
	}
	#define relopas(o, fn) tpl tmint fn(tmint& x, tmint y) {x = x o y; return x;}\
	tpl tmint fn(tmint& x, ll y) {return x = x o tmint(y);}
	relopas(+, operator+=) relopas(-, operator-=) relopas(*, operator*=) relopas(/, operator/=)
	#undef relopas
	tpl tmint quadres(tmint a) {
		tmint u, t;
		do {
			u = rd();
			t = u * u - a;
		} while (qpow(t, (tmint::mod() - 1) / 2) != -1);
		pair<tmint, tmint> ret = mkp(tmint(1), tmint(0)), mul = mkp(tmint(u), tmint(1));
		ll pw = (tmint::mod() + 1) / 2;
		#define im(x, y) mkp(x.first * y.first + x.second * y.second * t, x.first * y.second + x.second * y.first)
		for (; pw; pw >>= 1) {
			if (pw & 1) {
				ret = im(ret, mul);
			}
			mul = im(mul, mul);
		}
		if ((0-ret.first).val < ret.first.val) {
			return 0-ret.first;
		}
		return ret.first;
	}
}

namespace polyn {
	#define polymod 998244353
	using mint = modint::mint<polymod>;
	#define G mint(3)
	using vmint = vector<mint>;
//	#define NEWSTRUCT
	#ifdef NEWSTRUCT
	struct poly : vmint {
		using vmint::vmint;
	}; 
	#else
	using poly = vector<mint>;
	#endif
	poly& q0(poly& x) {
		while (!x.empty() && x.back() == 0) {
			x.pop_back();
		}
		return x;
	}
	poly modpower(poly f, ll n) {
		f.resize(n);
		return f;
	}
	void cut(poly& f, ll n) {
		f.resize(n);
	}
	void diff(poly& f) {
		for (ll i = 1; i < f.size(); ++i) {
			f[i - 1] = i * f[i];
		}
		f.pop_back();
	}
	void inte(poly& f) {
		f.empb(0);
		for (ll i = f.size() - 2; i >= 0; --i) {
			f[i + 1] = f[i] / (i + 1);
		}
		f[0] = 0;
	}
	#define _log2(x) (63^__builtin_clzll(x))
	mint gmem[200][4];
//	#define bitrevprecalc
	#ifdef bitrevprecalc
	u64 bitrev[4000005];
	u64 getbitrev(u64 x, ll p) {
		return bitrev[x];
	}
	#else
	inline u64 bitrev(u64 x) {
	   x = ((x >> 1) & 0x5555555555555555ull) | ((x & 0x5555555555555555ull) << 1);
	   x = ((x >> 2) & 0x3333333333333333ull) | ((x & 0x3333333333333333ull) << 2);
	   x = ((x >> 4) & 0x0f0f0f0f0f0f0f0full) | ((x & 0x0f0f0f0f0f0f0f0full) << 4);
	   x = ((x >> 8) & 0x00ff00ff00ff00ffull) | ((x & 0x00ff00ff00ff00ffull) << 8);
	   x = ((x >> 16) & 0x0000ffff0000ffffull) | ((x & 0x0000ffff0000ffffull) << 16);
	   x = (x >> 32) | (x << 32);
	   return x;
	}
	u64 getbitrev(u64 x, ll p) {
		return bitrev(x) >> (64 - _log2(p));
	}
	#endif
	void NTT(poly& f, i32 m = 1) {
		i32 ps = f.size();
		i32 pslg = _log2(ps);
		assert(1ll << pslg == ps);
		#ifdef bitrevprecalc
		if (bitrev[1] != (ps >> 1)) {
			bitrev[0] = 0;
			bitrev[1] = ps >> 1;
			for (ll i = 2; i < ps; ++i) {
				bitrev[i] = (bitrev[i >> 1] >> 1) + ((i & 1) << pslg - 1);
			}
		}
		#endif
		for (ll i = 0; i < ps; ++i) {
			if (i < getbitrev(i, ps)) {
				swap(f[i], f[getbitrev(i, ps)]);
			}
		}
		for (ll nlg = 1; nlg <= pslg; ++nlg) {
			ll N = 1ll << nlg;
			mint mul;
			if (gmem[nlg][m + 1].val) {
				mul = gmem[nlg][m + 1];
			} else {
				gmem[_log2(N)][m + 1] = mul = (m == 1 ? modint::qpow(G, (polymod - 1) >> nlg) : mint(1) / modint::qpow(G, (polymod - 1) >> nlg));
			}
			for (ll k = 0; k < ps; k += N) {
				mint g = 1;
				for (ll i = 0; i < (N >> 1); ++i) {
					mint ta = f[i + k] + g * f[i + k + (N >> 1)];
					mint tb = f[i + k] - g * f[i + k + (N >> 1)];
					f[i + k] = ta;
					f[i + k + (N >> 1)] = tb;
					g *= mul;
				}
			}
		}
	}
	void INTT(poly& f) {
		i32 ps = f.size();
		assert(1ll << _log2(ps) == ps);
		NTT(f, -1);
		for (auto& i : f) {
			i /= ps;
		}
	} 
	poly operator*(poly f, poly g) {
		i32 ps = 1;
		while (ps < 2 * max(f.size(), g.size())) {
			ps <<= 1;
		}
		cut(f, ps);
		cut(g, ps);
		NTT(f);
		NTT(g);
		for (ll i = 0; i < ps; ++i) {
			f[i] *= g[i];
		}
		INTT(f);
		return q0(f);
	}
	poly operator*(poly f, ll g) {
		for (auto& i : f) {
			i *= g;
		}
		return f;
	}
	poly operator*(ll f, poly g) {
		return g * f;
	}
	poly operator+(poly f, poly g) {
		poly ret;
		i32 ps = max(f.size(), g.size());
		f.resize(ps);
		g.resize(ps);
		for (ll i = 0; i < ps; ++i) {
			ret.empb(f[i] + g[i]);
		}
		return q0(ret);
	}
	poly operator-(poly f, poly g) {
		return f + -1 * g;
	}
	#define relop(x) poly operator x(poly f, ll g) {return f x poly({g});} poly operator x(ll f, poly g) {return poly({f}) x g;}
	#define relopm(x) poly operator x(poly f, mint g) {return f x g.val;} poly operator x(mint f, poly g) {return f.val x g;}
	relop(+) relop(-) relopm(+) relopm(-) relopm(*)
	poly operator/(poly f, mint g) {
		return f * (mint(1) / g);
	}
	poly operator/(poly f, ll g) {
		return f / mint(g);
	}
	poly invp(poly A, ll N) {
		cut(A, N);
		poly f = {mint(1) / A[0]};
		for (ll n = 2; n < (N << 1); n <<= 1) {
			f = 2 * f - f * f * modpower(A, n);
			cut(f, n);
		}
		cut(f, N);
		
		return f;
	}
	poly sqrtp(poly A, ll N) {
		cut(A, N);
		poly f = {quadres(A[0])};
		for (ll n = 2; n < (N << 1); n <<= 1) {
			f = f - (f * f - modpower(A, n)) * invp(f * 2, n);
			cut(f, n);
		}
		cut(f, N);
		return f;
	}
	poly lnp(poly A, ll N) {
		assert(A[0] == 1);
		poly ret = A;
		diff(ret);
		ret = ret * invp(A, N);
		inte(ret);
		ret[0] = 0;
		cut(ret, N);
		return ret;
	}
	poly expp(poly A, ll N) {
		assert(A[0] == 0);
		cut(A, N);
		poly f = {1};
		for (ll n = 2; n < (N << 1); n <<= 1) {
			f = f * (1 + modpower(A, n) - lnp(f, n));
			cut(f, n);
		}
		f[0] = 1;
		cut(f, N);
		return f;
	}
	poly powp(poly A, ll k, ll N) {
		assert(A[0] == 1);
		return expp(k * lnp(A, N), N);
	}
	poly powp(poly A, ll k, ll kphi, bool ktoobig, ll N) {
		if (ktoobig && A[0] == 0) {
			return poly(N, 0);
		}
		if (A[0] != 0) {
			return modint::qpow(A[0], kphi) * expp(k * lnp(A / A[0], N), N);
		}
		ll sft;
		for (sft = 0; sft < A.size(); ++sft) {
			if (A[sft].val) {
				break;
			}
		}
		for (ll i = 0; i + sft < A.size(); ++i) {
			A[i] = A[i + sft];
		}
		A.resize(A.size() - sft);
		poly ret = modint::qpow(A[0], kphi) * expp(k * lnp(A / A[0], N), N);
		sft *= k;
		if (sft >= N) {
			return poly(N, 0);
		}
		for (ll i = ret.size() - 1; i >= 0; --i) {
			if (i >= sft) {
				ret[i] = ret[i - sft];
			} else {
				ret[i] = 0;
			}
		}
		return ret;
	}
	poly slowpowp(poly A, ll k) {
		poly mul = A, ret = {1};
		for (ll i = 1; i <= k; i <<= 1) {
			if (k & i) {
				ret = ret * mul;
			}
			mul = mul * mul;
		}
		return ret;
	}
	#undef polymod
	#undef G
	#undef relop
}
using namespace polyn;

csep ll MAXN = 305;

string s;
ll dp[MAXN][MAXN], ci[MAXN], cw[MAXN];

void init() {}
int mian() {
	cin >> s;
	ll n = s.size();
	s = '.' + s;
	for (ll i = 1; i <= n; ++i) {
		ci[i] = ci[i - 1] + (s[i] == 'i');
		cw[i] = cw[i - 1] + (s[i] == 'w');
	}
	for (ll len = 1; len <= n; ++len) {
		for (ll i = 1; i + len - 1 <= n; ++i) {
			ll j = i + len - 1;
			if (s[i] == 'i' && s[j] == 'i' && len % 3 == 0 && ci[j - 1] - ci[i] == 2 * (cw[j - 1] - cw[i] - 1) && dp[i + 1][j - 1] == j - i - 2) {
				dp[i][j] = j - i + 1;
			}
			for (ll k = i; k < j; ++k) {
				dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
			}
		}
	}
	cout << dp[1][n] / 3 << endl;
	return 0;
}

#define OFFSYNC
//#define MULTITEST
int main() {
	#ifdef OFFSYNC
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	#endif
	#ifdef MULTITEST
	i32 T;
	cin >> T;
	while (T--) {
		init();
		mian();
	}
	#else
	mian();
	#endif
	return 0;
}
原文