标签:als tst col || input eof 返回 算法 put
1 package com.Liuyt; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.util.ArrayList; 5 import java.util.List; 6 import java.util.Scanner; 7 8 public class Main { 9 public static List<String> list = new ArrayList<String>(); 10 public static void permu(char[] str) { 11 if (str == null) { 12 return; 13 } 14 permu(str, 0); 15 } 16 17 private static void permu(char[] str, int begin) { 18 if (begin == str.length) { 19 list.add(String.valueOf(str)); 20 } else { 21 for (int i = begin; i < str.length; i++) { 22 // 首次for循环时候i=begin,即a,b,c分别和自身交换 23 char temp = str[begin]; 24 str[begin] = str[i]; 25 str[i] = temp; 26 27 permu(str, begin + 1); 28 // 采用递归调用,每次begin+1后 带入新的递归 29 /* 交换一遍后再交换一次,能够保证最后的到的还是原数组,好办法! */ 30 temp = str[begin]; 31 str[begin] = str[i]; 32 str[i] = temp; 33 } 34 } 35 } 36 37 public static void main(String[] args) throws FileNotFoundException { 38 Scanner sc = new Scanner(System.in); 39 40 String input = sc.nextLine(); 41 String s1 = input.split(" ")[0]; 42 String s2 = input.split(" ")[1]; 43 44 if (s2.length() < s1.length() || s2 == null){ 45 System.out.println("false"); 46 return; 47 }else if(s1 == null && s2 == null){ 48 System.out.println("true"); 49 } 50 char[] s1_char = s1.toCharArray(); 51 permu(s1_char); 52 53 for (int i = 0 ;i < list.size();i++){ 54 System.out.println(list.get(i)); 55 if (s2.contains(list.get(i))){ 56 System.out.println("true"); 57 return; 58 } 59 } 60 System.out.println("false"); 61 } 62 }
示例1:
ab dshfsjbaooo
true
示例2:
ab dsjklacbsoo
false
算法题:字符串s1,s2,判断s1的任意排列是否是s2的子串,返回true或false
标签:als tst col || input eof 返回 算法 put
原文地址:https://www.cnblogs.com/Liuyt-61/p/14635319.html