相对开音节
相对开音节构成的结构为辅音+元音(aeiou)+辅音(r除外)+e,常见的单词有bike、cake等。
给定一个字符串,以空格为分隔符,反转每个单词中的字母,若单词中包含如数字等其他非字母时不进行反转。
反转后计算其中含有相对开音节结构的子串个数(连续的子串中部分字符可以重复)。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
// a e i o u 第二个字符
// b c d f g h j k l m n p q r s t v w x y z 第一个字符
// b c d f g h j k l m n p q s t v w x y z 第三个字符
static List<Character> firstChar = Arrays.asList('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z');
static List<Character> secondChar = Arrays.asList('a','e','i','o','u');
static List<Character> thirdChar = Arrays.asList('b','c','d','f','g','h','j','k','l','m','n','p','q','s','t','v','w','x','y','z');
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
if (!(input.length() <10000)) {
System.out.println(0);
}
String[] inputStrings = input.split(" ");
List<String> reverseStr = new ArrayList<>();
// System.out.println(Arrays.toString(inputStrings));
for (int i = 0; i< inputStrings.length; i++) {
if (inputStrings[i].matches("[a-z]+")) {
reverseStr.add(new StringBuilder(inputStrings[i]).reverse().toString());
} else {
reverseStr.add(inputStrings[i]);
}
}
// System.out.println(reverseStr);
int result = 0;
for (String str : reverseStr) {
if (str.length() < 4) {
continue;
}
int index = str.indexOf('e');
while (index > -1) {
if (index < 3) {
index = str.indexOf('e',index + 1);
continue;
}
String needMatch = str.substring(index - 3, index + 1);
// System.out.println(needMatch);
if (isMatch(needMatch)) {
result++;
// System.out.println("needMatch:" + needMatch +", result:" + result);
}
index = str.indexOf('e',index + 1);
// System.out.println("index:" + index);
}
}
System.out.println(result);
}
//a b c d e f g h i j k l m n o p q r s t u v w x y z
// a e i o u 第二个字符
// b c d f g h j k l m n p q r s t v w x y z 第一个字符
// b c d f g h j k l m n p q s t v w x y z 第三个字符
private static boolean isMatch(String needMatch) {
char[] chars = needMatch.toCharArray();
return firstChar.contains(chars[0]) && secondChar.contains(chars[1]) && thirdChar.contains(chars[2]);
}
}
海康威视公司福利 1257人发布