题解 | #nico和niconiconi#
nico和niconiconi
http://www.nowcoder.com/practice/70a03345bae6499ea4338ebc3a0b60e9
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
String[] split=line.split(" ");
int n=Integer.parseInt(split[0]);
int a=Integer.parseInt(split[1]);
int b=Integer.parseInt(split[2]);
int c=Integer.parseInt(split[3]);
String line1=sc.nextLine();
long res = run1(n, a, b, c, line1);
System.out.println(res);
}
public static long run1(int n, int a, int b, int c, String s) {
if (s == null || s.length() < 4) {
return 0;
}
long[] dp = new long[n];
for (int i = 3; i < n; i ++) {
dp[i] = dp[i - 1];
if (i >= 3 && s.substring(i- 3, i + 1).equals("nico")) {
dp[i] = Math.max(dp[i], dp[i - 3] + a);
}
if (i >=5 && s.substring(i - 5, i + 1).equals("niconi")) {
dp[i] = Math.max(dp[i], dp[i - 5] + b);
}
if (i >= 9 && s.substring(i - 9, i + 1).equals("niconiconi")) {
dp[i] = Math.max(dp[i], dp[i - 9] + c);
}
}
return dp[n - 1];
}
}