有一小写英文字符构成的字符串,想得到一美丽子串,美丽子串的定义为:不能有连续有限个相同字符的的串。每个字符的个数用Ca,Cb。。。。Cz代替,字符串中。
输入:
第一行包含一个字符串s,1<=s的长度<=10^5
第二行包含26个整数表示0<=Ca,Cb...Cz<=10^9
输出:
输出最长美丽子串的长度
样例输入:
aabaacaa
10 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
样例输出:
5
代码如下:
import java.util.Scanner;
/**
* 漂亮无重复子串
*/
public class subString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] bound = new int[26];
int[] cnt = new int[26];
String s = sc.nextLine();
int n = s.length();
int max=0;
for(int i=0;i<26;i++){
bound[i]=sc.nextInt();
}
int i=0;
int j=0;
while(i<n){
cnt[s.charAt(i) - 'a']++;
if ((cnt[s.charAt(i) - 'a']) <= bound[s.charAt(i) - 'a'])
{
if (i - j + 1 >= max)
max = i - j + 1;
i++;
}
else
{
while ((cnt[s.charAt(i) - 'a'] )> bound[s.charAt(i) - 'a'])
{
cnt[s.charAt(j) - 'a']--;
j++;
}
i++;
}
}
System.out.println(max);
}
}