A. diverse substring (prefix and)
Question: Give a string, find a substring that meets the requirements of each letter in the substring to appear no more than the length of the substring/2, the length of the string n <1000.
Question: the starting and ending points of the N-party enumerative substrings. Create a prefix for each letter and you will be able to know the number of each letter in any string.
Code:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char s[1010];int n;int num[30][1010];int main(){ scanf("%d", &n); scanf("%s", s+1); for(int i = 1; i<=n; i++){ for(int k = 0; k<27; k++){ num[k][i] = num[k][i-1]; } num[s[i]-‘a‘][i] = num[s[i]-‘a‘][i-1]+1; } int l = -1, r = -1; for(int i = 1; i<=n; i++){ for(int j = 1; j<=i; j++){ bool flag = true; for(int k = 0; k<28; k++){ if(num[k][i]-num[k][j-1]>(i-j+1)/2){ flag = false; break; } } if(flag == true){ l = j; r = i; } } } if(l!=-1 && r!=-1){ cout<<"YES"<<endl; for(int i = l; i<=r; i++){ cout<<s[i]; } cout<<endl; } else{ cout<<"NO"<<endl; } return 0;}
Educational codeforces round 53 (rated for div. 2)