求条,玄3关,

P1026 [NOIP2001 提高组] 统计单词个数

这道题我也快红温了,怕不是错题吧,本地样例全过,但是就是过不去MLE,TLE横行
by 120229xhj @ 2024-08-24 09:12:02


@[limingyuan333](/user/889917) ```cpp #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i = a; i <= b; i++) using namespace std; const ll M = 1000005; inline void read(ll &a) { ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();} a = x*f; } ll p,k,s,len,f[210][55],sum[210][210]; string s2[10], sums="0"; bool check(ll l, ll r){ string ss=sums.substr(l,r-l+1); rep(i,1,s) if(ss.find(s2[i])==0) return 1; return 0; } void init(){ string s1; read(p), read(k); rep(i,1,p) cin >> s1, sums+=s1; len=sums.length()-1; read(s); rep(i,1,s) cin >> s2[i]; for(int i=len;i>=1;i--){ for(int j=i;j>=1;j--){ sum[j][i]=sum[j+1][i]; if(check(j,i)) sum[j][i]++; } } } void dp(){ rep(i,1,k) f[i][i]=f[i-1][i-1]+sum[i][i]; rep(i,1,len) f[i][1]=sum[1][i]; for(ll i=1;i<=len;i++){ for(ll j=1;j<=min(k,i);j++){ for(int l=j;l<i;l++){//!!!!!!!! f[i][j]=max(f[l][j-1]+sum[l+1][i],f[i][j]); } } } } int main(){ init(); dp(); cout << f[len][k] << endl; return 0; } ```
by yunyu2 @ 2024-09-15 20:12:34


|