码迷,mamicode.com
首页 > 其他好文 > 详细

多个字符串数组求相同数据及出现次数

时间:2014-06-22 23:06:07      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:class   blog   java   ext   com   get   

        String[] str = {"12","121"};
        String[] str1 = {"12","121"};
        String[] str2 = {"12","121","122"};
        String[] str3 = {"123","1234","125","126","111"};
        String[] str4 = {"1234","126","125","1232","111"};
        String[] str6 = {"12","121","222","1433","1234","126","125","1232"};


12,121,122 一块儿出现 2次
1234,125,126一块儿出现3次
1234,126,125,111 一块儿出现2次

package csdn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 自己定义一个数据类型,用来模拟二进制计算
 * @author sztsiang
 *
 */
class Num{
	private byte[] data;
	private int length;
	Num(int length){
		data = new byte[length];
		this.length = length;
	}
	public boolean increase(){
		for(int i = length -1 ; i >= 0; i--){
			if(i == 0 && data[0] == 1) return false;
			if(data[i] == 1) data[i] = 0;
			else{
				data[i] = 1;
				break;
			}
		}
		return true;
	}
	public byte get(int index){
		return data[index];
	}
	public int getCount(){
		int n = 0;
		for(int i = 0 ; i < length; i ++)
			if(data[i] == 1) n++;
		return n;
	}
}

/**
 * 对于每一组字符串,把每个字符串的组信息保存到set中,
 * 也就是一个字符串在哪几个set中出现就记录下来了,再把字符串和set用map保存起来。
 * 对于一个字符串数组,遍历每一个字符串,并到map中拿所在的组号。
 * 把这些组号最交集就是出现的次数
 * 不知道这样想对不起以及效率高不高。
 * @author sztsiang
 *
 */
public class EqualAndCount {
	
	private int count = 0;
	Map<String, Set<Integer>> map = new HashMap<String, Set<Integer>>();
	
	private void add(String key){
		Set<Integer> set = null;
		if(map.containsKey(key)){
			set = map.get(key);
			set.add(count);
			map.put(key, set);
		}else{
			set = new HashSet<Integer>();
			set.add(count);
			map.put(key, set);
		}
	}
	
	/**
	 * 添加
	 * @param strs
	 */
	public void add(String[] strs){
		count++;
		for(String str : strs)
			add(str);
	}
	
	/**
	 * 返回一个key值对应的set
	 * @param key
	 * @return Set<Integer>
	 */
	private Set<Integer> getSet(String key){
		return map.get(key);
	}
	/**
	 * 传入两个Set<Integer>
	 * 返回一个Set同时拥有两者相同部分数据
	 * @param set1
	 * @param set2
	 * @return Set<Integer>
	 */
	public Set<Integer> combineSet(Set<Integer> set1, Set<Integer> set2){
		
		Set<Integer> set = new HashSet<Integer>();
		
		Iterator<Integer> ite = set2.iterator();//优化,哪个小遍历哪个?
		
		while(ite.hasNext()){
			Integer t_value = ite.next();
			if(set1.contains(t_value)){
				//System.out.println("contains:"+ t_value);
				set.add(t_value);
			}
		}
		return set;
	}
	/**
	 * 返回一个字符串数组对应的符合次数
	 * @param strs
	 * @return count
	 */
	public int getEqualCount(String[] strs){
		Set<Integer> set = null;
		for(String str : strs){
			Set<Integer> t_set = getSet(str);
			set = (set == null ? t_set : combineSet(set, t_set));
		}
		return set.size();
	}

	/**
	 * 把字符串数组变成一个字符串
	 * @param args
	 * @return String
	 */
	private String ArraysToString(String args[]){
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		int length = args.length;
		for(int i = 0 ; i < length -1; i++){
			sb.append(args[i] +", ");
		}
		sb.append(args[length-1]);
		sb.append("]");
		return sb.toString();
	}
	/**
	 * 打印
	 * @param str
	 * @param count
	 */
	private void printResult(String str, int count){
		if(count > 1)//当大于一次的时候输出,如果想都输出就不写if
			System.out.println(str + "==>" + count);
	}
	/**
	 * 生成key的各种组合可能,并计算该组合出现的次数
	 */
	public void Traversal(){
		List<String> list  = new ArrayList<String>(map.keySet());
		int list_size = list.size();
		Num n = new Num(list.size());
		int index = 0;
		String str[];
		while(n.increase()){
			index = 0;
			int t = n.getCount();
			str = new String[t];
			for(int i = 0; i < list_size; i++){
				if(n.get(i) == 1) str[index++] = list.get(i);
			}
			int equal_count = getEqualCount(str);
			printResult(ArraysToString(str), equal_count);
		}
	}
	/**
	 * main
	 * @param args
	 */
	public static void main(String args[]){
		String[] str = {"12","121"};
        String[] str1 = {"12","121"};
        String[] str2 = {"12","121","122"};
        String[] str3 = {"123","1234","125","126","111"};
        String[] str4 = {"1234","126","125","1232","111"};
        String[] str6 = {"12","121","222","1433","1234","126","125","1232"};
		EqualAndCount eac = new EqualAndCount();
		eac.add(str);
		eac.add(str1);
		eac.add(str2);
		eac.add(str3);
		eac.add(str4);
		eac.add(str6);
		eac.Traversal();//自己组合所有的可能,然后查看次数
		System.out.println(eac.getEqualCount(str));
		System.out.println( eac.getEqualCount(str2));
		System.out.println(eac.getEqualCount(new String[]{"1234", "126", "125","111"}));
		System.out.println(eac.getEqualCount(new String[]{"1234", "126", "125"}) + "<<<");
		System.out.println(eac.getEqualCount(new String[]{"111"}));
		System.out.println(eac.getEqualCount(new String[]{"1433","111"}));
		
	}
}

 自己想的办法实现的,正确性未验证,效率都没考虑……毕竟是自己想出来的,就在这里记一下……

多个字符串数组求相同数据及出现次数,布布扣,bubuko.com

多个字符串数组求相同数据及出现次数

标签:class   blog   java   ext   com   get   

原文地址:http://www.cnblogs.com/kischn/p/3800143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!