1009
考虑贪心,暴力枚举一位。
$o(676n)$
#include<bits/stdc++.h> using namespace std; const int N=1e5+333; int n,m,zl; int pos[26],cnt[N],t[26],az[N]; char s[N],st[N]; int l[N],r[N],nx[N],zzq[26]; int main(){ios::sync_with_stdio(0);//freopen("p.in","r",stdin);//freopen("p.out","w",stdout);while(cin>>s){n=strlen(s);cin>>m;for(int i=0;i<26;i++)cin>>l[i]>>r[i];//if(m>10)continue;int ans=0;for(int i=0;i<=25;i++){ans+=l[i];pos[i]=n+3;t[i]=0;cnt[n+3]=0;} for(int i=0;i<=n+20;i++)az[i]=cnt[i]=0;for(int i=n;i>=1;i--){int dot=s[i-1]-'a';nx[i]=pos[dot];pos[dot]=i;t[dot]++; cnt[i]=t[dot];}zl=0;/*while(pos[0]<=n){cout<<pos[0]<<g' '<<cnt[pos[0]]<<endl;pos[0]=nx[pos[0]];}*/for(int i=1;i<=m;i++){st[i-1]='0';for(int j=0;j<=25;j++){int len=n-pos[j]; if(len+i<m)continue;if(az[j]==r[j])continue;bool ax=0;// AZAZAZAZfor(int k=0;k<=25;k++)zzq[k]=pos[k];int dis=pos[j];for(int k=0;k<=25;k++)while(pos[k]<=dis)pos[k]=nx[pos[k]];l[j]--;for(int k=0;k<=25;k++){int num=cnt[pos[k]];// if(pos[k]==i&&k!=j)num--;if(num<l[k])ax=1;// if(l[k]>m-i&&k!=j)ax=1; }int q=0;for(int pp=0;pp<=25;pp++)if(l[pp]>0)q+=l[pp]; //cout<<q<<endl;if(q>m-i)ax=1;if(ax){for(int k=0;k<=25;k++)pos[k]=zzq[k];l[j]++;continue;}//AFAEFAEEFDaz[j]++;st[i-1]='a'+j;//cout<<pos[j]<<endl;break;}//cout<<zl<<endl;//if(!zl){break;cout<<m<<' ';}if(st[i-1]=='0')zl=1;}if(zl){cout<<-1<<endl;continue;}for(int i=0;i<m;i++)cout<<st[i];cout<<endl;}return 0; }
1001
考虑dp
https://www.cnblogs.com/DeaphetS/p/11229389.html
#include<bits/stdc++.h> using namespace std; #define ll long long const int mo=998244353; ll dp[102][102][102][2],n,m,ans; vector<int>r[102],x[102]; int main(){int t;//freopen("p.in","r",stdin);//freopen("p.out","w",stdout);cin>>t;while(t){t--;cin>>n>>m;memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++){r[i].clear();x[i].clear();}for(int i=1;i<=m;i++){int a,b,c;cin>>a>>b>>c;r[b].push_back(a);x[b].push_back(c);}dp[0][0][0][0]=1;for(int cur=1;cur<=n;cur++){int o=cur%2;for(int i=0;i<=cur;i++)for(int j=i;j<=cur;j++)for(int k=j;k<=cur;k++)dp[i][j][k][o]=0;for(int i=0;i<=cur;i++)for(int j=i;j<=cur;j++)for(int k=j;k<=cur;k++){dp[i][j][cur-1][o]=(dp[i][j][cur-1][o]+dp[i][j][k][o^1])%mo;dp[i][k][cur-1][o]=(dp[i][k][cur-1][o]+dp[i][j][k][o^1])%mo;dp[j][k][cur-1][o]=(dp[j][k][cur-1][o]+dp[i][j][k][o^1])%mo;dp[i][j][k][o]=(dp[i][j][k][o]+dp[i][j][k][o^1])%mo;}for(int i=0;i<=cur;i++)for(int j=i;j<=cur;j++)for(int k=j;k<=cur;k++)for(int az=0;az<r[cur].size();az++){int ql=r[cur][az],qx=x[cur][az];if((i>=ql)+(j>=ql)+(k>=ql)+(cur>=ql)!=qx)dp[i][j][k][o]=0;}}ans=0;for(int i=0;i<=n;i++)for(int j=i;j<=n;j++)for(int k=j;k<=n;k++)ans=(ans+dp[i][j][k][n%2])%mo;cout<<ans<<endl;}return 0; }