[ABC 098] B-Cut and Count
B - Cut and Count
Time limit : 2sec / Memory limit : 1024MB
Score : 200 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters. We will cut this string at one position into two strings X and Y. Here, we would like to maximize the number of different letters contained in both X and Y. Find the largest possible number of different letters contained in both Xand Y when we cut the string at the optimal position.
Constraints
- 2≤N≤100
- |S|=N
- S consists of lowercase English letters.
[题目解释]
给你一个整数N(2≤N≤100),和一个长度为N的字符串S,把这个字符串在某一位置切割成两个字符串X,Y,求在包含在X,Y之中最大的不同字符数
[题目解析]
由于我们不知道在那个位置切割字符串S可以使包含X,Y中的不同字符数最大.所以ans记录已知存在X,Y中最大的不同字符数,枚举切割点,把当前位置中X,Y的不同字符数记录下来,若当前切割点的不同字符数比已知最大不同字符数大,那么更新ans中的答案,最后ans中记录的即为最大不同字符数
[代码]
/* Name: Cut and Count Author: FZSZ-LinHua Date: 2018 06 06 Exec time: 1ms Memory usage: 256KB Score: 200 Algorithm: Brute-force */ # include "iostream" # include "cstdio" # include "cstring" using namespace std; const int maxm=100+10; bool f[maxm], //f[i]表示切割在j位置时1~j是否存在i字符 q[maxm]; //q[i]表示切割在j位置时j+1~n是否存在i字符 int main(){ int n, //字符串长度 now, //当前不同字符数 ans; //已知最大不同字符数(初始值为0) register int i,j; char s[maxm]; scanf("%d",&n); //读入字符串的长度 scanf("%s",s); //读入的字符串 for(i=0;i<n;i++){ //枚举切割点 f[s[i]-'a']=true; //当前i位置的字符在f中标记为存在 memset(q,false,sizeof(q)); //初始化q数组 for(j=i+1;j<n;j++){ //当前j位置的字符在q中标记为存在 q[s[j]-'a']=true; } now=0; //初始化now for(j=0;j<26;j++){ if(f[j] && q[j]){ //如果两串中拥有相同字符那么记入到now中 now++; } } ans=max(ans,now); //若当前不同字符数大于已知不同字符则更新答案 } printf("%d",ans); //打印答案 return 0; }

浙公网安备 33010602011771号