Educational Codeforces Round 53 (Rated for Div. 2)
阿新 • • 發佈:2018-10-27
space iostream ace ted color 代碼 ces pan else
A. Diverse Substring(前綴和)
題意:給一個字符串,找出一個子串滿足該子串中每個字母出現的次數不超過子串的長度/2,字符串的長度n<1000.
題解:n方枚舉子串的起點和終點,對於每個字母建一個前綴和就能知道在任意一個字串中每個字母出現的個數了。
代碼:
#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)