制造回文

xiaoxiao2021-02-28  100

题目: 牛牛有一些字母卡片,每张卡片上都有一个小写字母,所有卡片组成一个字符串s。牛牛一直认为回文这种性质十分优雅,于是牛牛希望用这些卡片拼凑出一些回文串,但是有以下要求: 1、每张卡片只能使用一次 2、要求构成的回文串的数量最少 牛牛想知道用这些字母卡片,最少能拼凑出多少个回文串。 例如: s = “abbaa”,输出1,因为最少可以拼凑出”ababa”这一个回文串 s = “abc”, 输出3,因为最少只能拼凑出”a”,”b”,”c”这三个回文串 输入描述:

输入包括一行,一个字符串s,字符串s长度length(1 ≤ length ≤ 1000). s中每个字符都是小写字母 输出描述:

输出一个整数,即最少的回文串个数。 示例1 输入 abc 输出 3

不多说上代码看注:(思路:根据回文特征,只需判断字符串中个数为奇数的字符个数即可,个数为1,则回文至少1个,若不为1,则回文即字符个数)

`public class HuiWen {

/** * @param args the command line arguments * */ public static int HuiWenstr(String str){ int len=str.length(); int result=0; int[] ch=new int[256]; for(int i=0;i<len;i++){ ch[str.charAt(i)]++; } for(int j=96;j<123;j++){ if(ch[j]%2==1){ System.out.println(j); result++; }` } return result==1?1:result; } public static void main(String[] args) { // TODO code application logic here Scanner scan=new Scanner(System.in); String test=scan.nextLine(); System.out.println(HuiWenstr(test)); }

} 注:在ASCII字符表中大写字母编码为65~90,小写字母编码为97~122。

转载请注明原文地址: https://www.6miu.com/read-61170.html

最新回复(0)