package com.gdh.backtext;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class BackText {
String text;

public BackText() {
  super();
  this.text = null;
}

public BackText(String text) {
  super();
  this.text = text;
}

public boolean isBackText(){
  for(int i=0,j=text.length()-i-1;i<=j;i++,j--){
    if( text.charAt(i) != text.charAt(j) ){
      return false;
    }
  }
  return true;
}

public Map<Character,Integer> countString(){

  Map<Character,Integer> map=new HashMap<>();
  int count=0;

  String temp=new String();
  for(int i=0;i< text.length();i++){
    if ( temp.indexOf(text.charAt(i), 0) < 0){
      temp+=text.charAt(i);
    }
  }
  map.clear();
  for(int i=0;i< temp.length();i++){
    if(!map.containsKey(temp.charAt(i))){
      for(int j=0;j< text.length();j++){
        if(text.charAt(j) == temp.charAt(i) ){
          count++;
        }
      }
      map.put(temp.charAt(i), count);
      count=0;
    }
  }
  //循环打印
  for(Entry<Character,Integer> item:map.entrySet()){
    System.out.println("字符:" + item.getKey() + " 值:" + item.getValue());
  }
  return map;
}

public String convert(){

  int checksum = 0;
  int itemcount=0;

  Map<Character,Integer> map=countString();
  for(Entry<Character,Integer> item:map.entrySet()){
  checksum+=item.getValue();
  if( item.getValue() %2 != 0)
    itemcount++;
  }
  if( itemcount > 1 ){
    System.out.println("该字符串不能转换为回文字");
    return null;
  }

  StringBuffer temp=new StringBuffer(text);//线程安全
  //StringBuilder temp=new StringBuilder();//线程非安全
  int begIdx=0;
  int endIdx=checksum-1;
  Character key=null;
  boolean flag=false;
  for(Entry<Character,Integer> item:map.entrySet()){
  if( checksum % 2 ==0 ){
  for(int i=0;i<item.getValue()/2;i++){
    temp.setCharAt(begIdx++, item.getKey());
    temp.setCharAt(endIdx--, item.getKey());
  }
    }else{
      if(item.getValue()%2==0 ){
        for(int i=0;i<item.getValue()/2;i++){
          temp.setCharAt(begIdx++, item.getKey());
          temp.setCharAt(endIdx--, item.getKey());
        }
      }else{
        key=item.getKey();
        flag=true;
        continue;
      }
    }
  }
  if(flag)
  {
    for(int i=0;i<map.get(key);i++){
      temp.setCharAt(begIdx++, key);
    }
  }
  return temp.toString();
}

  public static void main(String[] args) {
    BackText bt=new BackText("1122334455667788990");
    if( !bt.isBackText() )
      System.out.println("该字符串不是回文字");
    else
      System.out.println("该字符串是回文字");
    String dest=new String();
    System.out.println("开始转换...");
    dest=bt.convert( ) ;
    System.out.print("转换后的结果为:");
    System.out.println(dest);
  }
}

回文字算法(java版本)的更多相关文章

  1. 通过“回文字算法”复习C++语言。

    一.什么是回文字 给定一个字符串,从前往后读和从后往前读,字符串序列不变.例如,河北省农村信用社的客服电话是“96369”,无论从后往前读,还是从前后往后读,各个字符出现的位置不变. 二.功能实现 ( ...

  2. 微博短链接的生成算法(Java版本)

    最近看到微博的短链接真是很火啊,新浪.腾讯.搜狐等微博网站都加入了短链接的功能.之所以要是使用短链接,主要是因为微博只允许发140 字,如果链接地址太长的话,那么发送的字数将大大减少.短链接的主要职责 ...

  3. POJ 3974 最长回文字串(manacher算法)

    题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...

  4. 最长回文字串——manacher算法

    时间复杂度:O(n) 参考:https://segmentfault.com/a/1190000003914228 1.问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字 ...

  5. java实现简单回文算法

    算法要求 编写一个程序,判断一个字符串是否为"回文".回文串:字符串字符从前往后与从后往前一致(中心对称). 算法思路 首先将字符串等分左右两块,然后依次对称比较每一对字符是否相同 ...

  6. 归并排序算法 java 实现

    归并排序算法 java 实现 可视化对比十多种排序算法(C#版) [直观学习排序算法] 视觉直观感受若干常用排序算法 算法概念 归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Di ...

  7. Atitit 电子商务订单号码算法(java c# php js 微信

    Atitit 电子商务订单号码算法(java c# php js  微信 1.1. Js版本的居然钱三爷里面没有..只好自己实现了. 1.2. 订单号标准化...长度16位 1.3. 订单号的结构 前 ...

  8. 求字符串的最长回文字串 O(n)

    昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...

  9. JGibbLDA:java版本的LDA(Latent Dirichlet Allocation)实现、修改及使用

    转载自:http://blog.csdn.net/memray/article/details/16810763   一.概述 JGibbLDA是一个java版本的LDA(Latent Dirichl ...

随机推荐

  1. Atitit apache 和guava的反射工具

    Atitit apache 和guava的反射工具 apache1 Spring的反射工具类 ReflectionUtils1 Guava 反射工具2 apache  34             7 ...

  2. 每天一个linux命令(11):nl命令

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...

  3. C# string.format、string.connect和+=运算 效率计算

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Stri ...

  4. Test Design Guidelines for Reusability

    Last Updated: JAN.10.2008 From: http://safsdev.sourceforge.net/sqabasic2000/TestDesignGuidelines.htm ...

  5. Entity Framework 5中应用表值函数进行Linq查询

    Entity Framework 5引入了表值函数(Table-Valued Functions TVFs).表值函数的返回类型是一个Table类型,可用在SQL查询语句中.最简单的表值函数,读取客户 ...

  6. JUnit4使用

    1.导入Junit4jar包: Eclipse中在项目上右键点击Bulid Path,然后再点击Add libraries,选择JUnit 2.初次使用 首先先创建一个java项目如下: Demo.j ...

  7. [algorithm] My rookie plan to start

    若干年后,经验有一些,但根基不牢靠.[algorithm] series 借助学习Standard Template Library: Algorithms的这段时期,在自己的算法和c++基础方面加些 ...

  8. 学习ng2,从zonejs开始(非官方翻译) ----angular2系列(一)

    Zone是什么: 官方解释:zone.js为JavaScript提供了执行上下文,可以在异步任务之间进行持久性传递. 最开始我一直没理解到这句话,学习过程中我也因为自己的一些失误而一直纠结徘徊,情况是 ...

  9. jQuery & CSS 制作金属质感的选择按钮

    如果能把 CSS 运用好,我们创作出好的交互和效果的可能性大大增加.这篇文章中,我想与大家分享一组结合 jQuery & CSS 制作的充满金属质感的选择框效果,绝对是超级精美的效果. 在线演 ...

  10. java实现将汉字转为拼音

    有时候为了方便操作程序的开发,需要将汉字转为拼音等操作.下面这个是自己结合网上的资料,加上自己在公司项目中的亲自实践.完整的实现了将汉字转为拼音的操作.这个Demo只是负责将其转换,在main方法中测 ...