所有子序列的回文数出现次数

输入一个字符串,判断这个字符串的子序列回文数个数,这个子序列可以不连续,但是要有前后次序,而且不同位置的元素就算一个例如XXY,输出4,因为X,X,Y,XX

  package test;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;


public class IStringBuffer {
	static List<List> listroot =new LinkedList<>();
	public static void main(String[] args) {
		String str = new String();
		String str1 = new String();
		Scanner in = new Scanner(System.in);
		str= in.nextLine();
		perm(str);
		System.out.println(listroot.size());
	}
	public static boolean pan(List str){
		if(str.size()==1){
			return true;
		}
		for(int i=0;i<str.size()/2;i++){
			if(str.get(i).equals(str.get(str.size()-1-i))){	
			}else{
				return false;
			}
		}
		return true;
	}
	
    public static void perm(String s) {  
        List<String> result = new ArrayList<String>(); 
        for (int i = 1; i <= s.length(); i++) {  
            perm(s, i, result);  
        }  
    }  
  
    public static void perm(String s, int m, List<String> result) {  
    		int j=0;
        if (m == 0) {  
            if(pan(result)){      	
            	listroot.add(result);
            }else{
            }
            return; 
        }  
        if (s.length() != 0) {   
            result.add(s.charAt(0) + "");  
            perm(s.substring(1, s.length()), m - 1, result);  
            result.remove(result.size() - 1);   
            perm(s.substring(1, s.length()), m, result);  
        }  
    }  
	
}




   

猜你喜欢

转载自blog.csdn.net/m0_37687058/article/details/79873667