日期:2014-05-20  浏览次数:20701 次

高分100 求代码 两行数据找相同元素
我有一个比如叫做input.txt 文本 格式如下
12 Follower[13,14,16,]
12 Following[14,29,39,]
13 Following[12,]
14 Follower[12,48,98,]
14 Following [12,48,90,]
15 Following[16]
16 Follower[15]

这个文本是按照第一列的数字(之后成为用户) 升序排列的
我想找的是 比如用户12 他既有follower 也有following 我想找方括号里面相同的元素 并输出存为output.txt 例如:

12 [14]
14 [12,48]

应该怎么做??
我的数据的第一列(用户id)是排序好了的 数值递增的
而第二列 则有的用户只有follower(如16)有的只有following(如13,15) 而有的是都有 譬如12,14

请给我具体的code 跪求!谢谢100分哟~~~

------解决方案--------------------
Java code
public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("d:\\input.txt");
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String astr = br.readLine();
        String bstr = null;
        
        Pattern p = Pattern.compile("\\d+");
        List lista = new ArrayList();
        List listb = new ArrayList();
        
        while ((bstr = br.readLine()) != null) {
            String[] aarray = astr.split(" ");
            String[] barray = bstr.split(" ");
            if (aarray[0].equals(barray[0]) && astr.contains("Follower")
                    && bstr.contains("Following")) {
                Matcher am = p.matcher(aarray[1]);
                while(am.find()){
                    lista.add(am.group());
                }
                Matcher bm = p.matcher(barray[1]);
                while(bm.find()){
                    listb.add(bm.group());
                }
                lista.retainAll(listb);

                if(lista.size() > 0){
                    System.out.print(barray[0] +" ");
                    System.out.print("[");
                    for(int i = 0;i < lista.size();i++){
                        System.out.print(lista.get(i));
                        if(i != lista.size() - 1){
                            System.out.print(",");
                        }
                    }
                    System.out.println("]");
                }
                lista.clear();
                listb.clear();
                
            }
            astr = bstr;
        }
        br.close();
    }

------解决方案--------------------
呵呵。我也来写一个,欢迎大家指评。
Java code

import java.io.*;
import java.util.*;
public class InputTest {
    public static void main(String[] args) throws IOException {
        File file = new File("D:/input.txt");
        Scanner in = new Scanner(file);
        Map<String, String> map_root = new HashMap<String, String>();
        Map<String, String> map_rs = new HashMap<String, String>();
        String[] str;String str1;
        while (in.hasNextLine()) {
            str = in.nextLine().split(" ");
            if (map_root.containsKey(str[0]))     map_root.put(str[0], map_root.get(str[0]) + " " + str[1]);
            else map_root.put(str[0], str[1]);
        }
        for (String key : map_root.keySet()) {
             str1 = map_root.get(key);
             String[] str2,str5,str6;String str3,str4;
            if (str1.indexOf(" ") != -1) {
                 str2 = str1.split(" ");
                 str3 = str2[0].substring(str2[0].indexOf("[") + 1, str2[0].lastIndexOf("]"));
                 str4 = str2[1].substring(str2[1].indexOf("[") + 1, str2[1].lastIndexOf("]"));
                 str5 = str3.split(",");
                 str6 = str4.split(",");
                for (int j = 0; j < str5.length; j++) {
                    if ("" == str5[j])    continue;
                    for (int p = 0; p < str6.length; p++) {
                        if ("" == str6[p])    continue;
                        if (str5[j].equals(str6[p])) {
                            if (map_rs.containsKey(key)) map_rs.put(key, map_rs.get(key) + "," + str5[j]);
                            else map_rs.put(key, str5[j]);
                        }
                    }
                }
            }
        }
        StringBuffer buf=new StringBuffer();
        for(String key:map_rs.keySet()){
            buf.append(key+" "+"["+map_rs.get(key)+"]"+"\r\n");
        }
        FileOutputStream os=new FileOutputStream(new File("d:/output.txt"));
        os.write(buf.toString().getBytes(), 0, buf.toString().getBytes().length);
    }
}