略;
一开始觉得2s,3e8是不是莽的过啊~(天真!可笑!)看来我是太爱尺取了!
预处理复杂度直接就减少了一个数量级,预处理num[ c ] [ rp ] (字符 c 在能替换 rp 数量个情况下的最长区间)复杂度O(26*N^2 + q);CC
#include
using namespace std;
#define din(var) scanf("%d",&var)
#define dout(var) printf("%d\n",var);
#define dinll(var) scanf("%lld",&var)
#define doutll(var) printf("%lld\n",var);
#define rep(i,a,n) for (int i=a;i
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define p_b push_back
#define m_p make_pair
#define aLL(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
typedef vector<int> VI;
typedef long double LD;
typedef long long LL;
typedef vector<long long> VIL;
template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF) return 0; //EOF
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
inline void out(LL x)
{
if(x>9) out(x/10);
putchar(x%10+'0');
}
inline void out(int x)
{
if(x>9) out(x/10);
putchar(x%10+'0');
}
inline int Mmax(int x, int y){
return x > y ? x : y;
}
int num[30][1507], n;
char s[1507];
int main(){
int len;
int q, m;
din(n);
scanf("%s", s+1);
rep(c, 0, 26){
rep(i, 1, n+1){
int rp = 0;
rep(j, i, n+1){
if((s[j]-'a') != c) rp++;
num[c][rp] = Mmax(num[c][rp], j-i+1);
}
}
rep(i, 1, n+1) num[c][i] = Mmax(num[c][i], num[c][i-1]);
}
din(q);
char op[5];
while(q--){
scan_d(m);
scanf("%s", op);
dout(num[op[0]-'a'][m]);
}
return 0;
}
段中的所有元素种类的个数=数组的所有元素种类的个数
选几个段,求最大的每个段的异或和;
DP[ Now ] = DP[ Pre ] + Val{[Pre, Now]};
#include
using namespace std;
#define din(var) scanf("%d",&var)
#define dout(var) printf("%d\n",var);
#define dinll(var) scanf("%lld",&var)
#define doutll(var) printf("%lld\n",var);
#define rep(i,a,n) for (int i=a;i
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define p_b push_back
#define m_p make_pair
#define aLL(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
typedef vector<int> VI;
typedef long double LD;
typedef long long LL;
typedef vector<long long> VIL;
template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF) return 0; //EOF
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
inline void out(LL x)
{
if(x>9) out(x/10);
putchar(x%10+'0');
}
inline void out(int x)
{
if(x>9) out(x/10);
putchar(x%10+'0');
}
inline int Mmax(int x, int y){return x > y ? x : y;}
inline LL Mmax(LL x, LL y){return x > y ? x : y;}
inline int Mmin(int x, int y){return x < y ? x : y;}
inline LL Mmin(LL x, LL y){return x < y ? x : y;}
const int Maxn = 5007;
int dp[Maxn], a[Maxn];
int Left[Maxn], Right[Maxn], n, vis[Maxn];
int main(){
din(n);
rep(i, 0, 5001) Left[i] = n, Right[i] = 1;
rep(i, 1, n+1) din(a[i]), Right[a[i]] = Mmax(Right[a[i]], i), Left[a[i]] = Mmin(Left[a[i]], i);
rep(i, 1, n+1){
dp[i] = dp[i-1];
mem(vis, 0);
int st = i;
int ans = 0;
for(int j=i;j>0;j--){
if(!vis[a[j]]){
if(Right[a[j]] > i) break;
if(Left[a[j]] < st) st = Left[a[j]];
ans = ans ^ a[j];
vis[a[j]] = 1;
}
if(j <= st) dp[i] = Mmax(dp[i], dp[j-1] + ans);
}
}
dout(dp[n]);
return 0;
}