提交时间:2025-10-18 13:48:12
运行 ID: 38627
#include<bits/stdc++.h> #define pii pair<int,int> #define fi first #define se second #define mk make_pair #define ll long long #define uint unsigned int #define ull unsigned long long using namespace std; const int N=510; bool f[N][N]; int n;string s; void slv(){ cin>>n;cin>>s;s=' '+s; memset(f,0,sizeof(f)); for(int i=1;i<=n+1;i++) f[i][i-1]=1; for(int i=n;i>=1;i--){ for(int j=i+1;j<=n;j++){ f[i][j]=0; if(s[i]==s[j]) f[i][j]=f[i+1][j-1]; for(int k=i+1;k<=j-1;k++){ if(s[i]==s[j]) f[i][j]|=f[i+1][k-1]&f[k+1][j-1]; } for(int k=i;k<=j;k++) f[i][j]|=f[i][k-1]&f[k+1][j]; //cout<<i<<' '<<j<<' '<<f[i][j]<<endl; } } if(f[1][n]) cout<<"YES"<<endl; else cout<<"NO"<<endl; } int main(){ //freopen("1.in","r",stdin); //freopen("1.out","w",stdout); ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t=1;cin>>t; while(t--) slv();cout.flush(); return 0; }