AcWing 856. 变量名拆分
原题链接
简单
作者:
ryan-157
,
2020-05-05 19:41:29
,
所有人可见
,
阅读 628
import java.util.*;
/**
* @author Ryan
* @data 2020/05/05 - 周二
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] p = sc.nextLine().split(" ");
Set<String> set = new HashSet<>();
int maxLen = 0;
for (int i = 0; i < p.length; i++) {
set.add(p[i]);
maxLen = Math.max(maxLen, p[i].length());
}
String t;
boolean[] dp = new boolean[s.length()+1];
dp[0] = true;
for (int i = 0; i < s.length(); i++) {
for (int j = i; j < s.length(); j++) {
if (j - i > maxLen) {
break;
}
t = s.substring(i,j+1);
if (set.contains(t) && dp[i]) {
dp[j + 1] = true;
// set.remove(t);
}
}
}
if (dp[s.length()]) {
System.out.println("True");
} else {
System.out.println("False");
}
}
}