赛迪网 > IT技术 Java > 技术动态
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

J2SE综合--浅析实现排列组合查询算法

发布时间:2008.01.30 04:51     来源:赛迪网    作者:Jegg

所谓的排列组合查询就相当于GOOGLE高级查询中“包含以下全部的字词”查询,也就是说查询中必须包含所有查询关键词,而且他们的顺序可以是任意。以下程序段实现了这一功能。比如输入查询关键字:tom tina则最一般的情况是在程序中使用类似于"select sex from student where name like '%tom%tina%' or name like '%tina%tom%' ordered by age" 的查询语句实现以上的查询,因此如何得到'%tina%tom%' 和'%tom%tina%' 就是该程序和算法要实现的.

首先想到的就是写出一个排列组合的算法,然后用该算法输出所要查询关键字的所有情况,比如 我输入了以下几个关键字: EGG APPLE TIME 则要写一个程序输出 这3个单词的所有排列情况,比如:EGG APPLE TIME 情况2 EGG TIME APPLE, 情况3 APPLE EGG TIME......不用说,大家一看就知道应该是3的阶乘种情况也就是1*2*3这里就不一一列出了。

写出一段程序,或者一个函数比如: public String paileizuhe(String inputstr){......} 该函数返回一个排列组合好的QUERY字符串,比如使用该函数并赋予他两个字符串参数(tom,tina)则:public String pailiezuhe("tom","tina");则输出: "select sex from student where name like '%tom%tina%' or name like '%tina%tom%' ordered by age "  这里,我们关心的是如何生成tom tina 的组合即'%tina%tom%' 和'%tom%tina%' 至于生成整个如上的字符串是非常简单的只要用StringBuffer将那些常量悬挂起来最后组合一下就可以了.以下程序给出了排列组合输出的实现:

import java.math.BigInteger;
import java.util.*;

public class PermutationGenerator {

    private int[] a;
    private BigInteger numLeft;
    private BigInteger total;
    public PermutationGenerator(int n) {
        if (n < 1) {
            throw new IllegalArgumentException("Min 1");
        }
        a = new int[n];
        total = getFactorial(n);
        reset();
    }

    //------
    // Reset
    //------

    public void reset() {
        for (int i = 0; i < a.length; i++) {
            a[i] = i;
        }
        numLeft = new BigInteger(total.toString());
    }

    //------------------------------------------------
    // Return number of permutations not yet generated
    //------------------------------------------------

    public BigInteger getNumLeft() {
        return numLeft;
    }

    //------------------------------------
    // Return total number of permutations
    //------------------------------------

    public BigInteger getTotal() {
        return total;
    }

    //-----------------------------
    // Are there more permutations?
    //-----------------------------

    public boolean hasMore() {
        return numLeft.compareTo(BigInteger.ZERO) == 1;
    }

    //------------------
    // Compute factorial
    //------------------

    private static BigInteger getFactorial(int n) {
        BigInteger fact = BigInteger.ONE;
        for (int i = n; i > 1; i--) {
            fact = fact.multiply(new BigInteger(Integer.toString(i)));
        }
        return fact;
    }

    //--------------------------------------------------------
    // Generate next permutation (algorithm from Rosen p. 284)
    //--------------------------------------------------------

    public int[] getNext() {

        if (numLeft.equals(total)) {
            numLeft = numLeft.subtract(BigInteger.ONE);
            return a;
        }

        int temp;

        // Find largest index j with a[j] < a[j+1]

        int j = a.length - 2;
        while (a[j] > a[j + 1]) {
            j--;
        }

        // Find index k such that a[k] is smallest integer
        // greater than a[j] to the right of a[j]

        int k = a.length - 1;
        while (a[j] > a[k]) {
            k--;
        }

        // Interchange a[j] and a[k]

        temp = a[k];
        a[k] = a[j];
        a[j] = temp;

        // Put tail end of permutation after jth position in increasing order

        int r = a.length - 1;
        int s = j + 1;

        while (r > s) {
            temp = a[s];
            a[s] = a[r];
            a[r] = temp;
            r--;
            s++;
        }

        numLeft = numLeft.subtract(BigInteger.ONE);
        return a;

    }
//程序测试入口
    public static void main(String[] args) {

        int[] indices;
        String[] elements = { "1", "2", "3" };
        PermutationGenerator x = new PermutationGenerator(elements.length);
        StringBuffer permutation;

        while (x.hasMore()) {
            permutation = new StringBuffer("%");
            indices = x.getNext();
            for (int i = 0; i < indices.length; i++) {
                permutation.append(elements[indices[i]]).append("%");
            }
            System.out.println(permutation.toString());

        }
    }

}

可以看到我们输入1 2 3 得到了他门所有的排列组合:
%1%2%3%
%1%3%2%
%2%1%3%
%2%3%1%
%3%1%2%
%3%2%1%
由此,我们可以很轻易的得到给定关键字的排列组合了.
需要注意的是,如果查询是输入关键字过多,比如5个则会有120中的组合,6个是720种,要是10个以上的话......所以该算法不适合很多关键字的全排列查询.

当然我的思路是最土和直接的,远不如GOOGLE,只是一种实现而已,如果文章对诸位有所帮助,便起到了作用。谁有更好的方法希望您也能共享出来。
          (责任编辑:包春林)


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· J2EE综合:深入谈论JSF与Struts的异同 (01-29) · 表现层框架Struts/Tapestry/JSF架构比较 (01-29)
· JAVA基础:webwork的基本配置与应用示例 (01-29) · 基础:Eclipse上的Tomcat插件安装和调试 (01-29)
· Java中不同类型的转换和提升 (01-29) · Java源码分析:深入探讨Iterator模式 (01-28)
· 在用户关掉web浏览器窗口前, 进行动作 (01-28) · 高速缓存和连接池对访问数据库性能影响 (01-28)
· J2SE综合:JAVA语言关于字符串替换的思考 (01-28) · 进阶:Tomcat 的数据库连接池设置与应用 (01-28)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
· 网银交易收费 我国银行业如何达国际化标准
· 家庭信息化普及率提高 网上缴费成为新时尚
· 五条黄金准则能够让CIO巧妙加薪 CIO焦虑调查
· 网上书店解决方案 深圳边检指挥中心ITSM项目
  IT博客 ·曾剑秋·项立刚·Java学习·网管
  IT技术论坛 ·开发·网管·安全·数据库·系统