提取字串

package cn.dlpu.lby;

public class TiqusubString {

	/*
	 * 串“abcba”以字母“c”为中心左右对称;串“abba” 是另一种模式的左右对称。
	 * 这两种情况我们都称这个串是镜像串。特别地,只含有1个字母的串,可以看成是第一种模式的镜像串。
	 * 一个串可以含有许多镜像子串。我们的目标是求一个串的最大镜像子串(最长的镜像子串),
	 * 如果有多个最大镜像子串,对称中心靠左的优先选中。例如:“abcdeefghhgfeiieje444k444lmn
	 * ”的最大镜像子串是:“efghhgfe”
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "abcdeefghhgfeiieje444k444lmn";
		System.out.println(getMaxString(str));

	}

	private static String getMaxString(String str) {
		// TODO Auto-generated method stub
		String max_string = "";
		for (int i = 0; i < str.length(); i++) {
			// 第一种模式
			int step = 1;
			try{
				for (;;) {
					if (str.charAt(i - step) != str.charAt(i + step))
						break;
					step++;
				}
			}catch (Exception e) {
			}
			String s1 = str.substring(i - step + 1, i + step);

			// 第二种模式
			step = 0;
			try{
				for (;;) {
					if (str.charAt(i - step) != str.charAt(i + step+1))
						break;
					step++;
				}
			}catch(Exception e){
			}
			String s2 = str.substring(i - step+1, i + step+1);
			
			if(s1.length()>max_string.length())
				max_string = s1;
			if(s2.length()>max_string.length())
				max_string = s2;
		}
		return max_string;
	}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值