微软2014实习生及秋令营技术类职位在线测试_题目4 : Most Frequent Logs

时间限制: 10000ms
单点时限: 3000ms
内存限制: 256MB

Description

In a running system, there are many logs produced within a short period of time, we'd like to know the count of the most frequent logs.

Logs are produced by a few non-empty format strings, the number of logs is N(1<=N<=20000), the maximum length of each log is 256.

Here we consider a log same with another when their edit distance (see note) is <= 5.

Also we have a) logs are all the same with each other produced by a certain format string b) format strings have edit distance  5 of each other.

Your program will be dealing with lots of logs, so please try to keep the time cost close to O(nl), where n is the number of logs, and l is the average log length.

Note edit distance is the minimum number of operations (insertdeletereplace a character) required to transform one string into the other, please refer to


http://en.wikipedia.org/wiki/Edit_distance for more details.



Input

Multiple lines of non-empty strings.

Output


The count of the most frequent logs.


样例输入
Logging started for id:1
Module ABC has completed its job
Module XYZ has completed its job
Logging started for id:10
Module ? has completed its job
样例输出
3

//source here


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{
    
    static int count = 0,maxcount = 0;
    static List<String> list = new ArrayList<String>();

    public static void main(String[] args) {
        
        int N = 0;
        int i=0,j=0,k=0;
        Boolean flag = false;
        @SuppressWarnings("resource")
        Scanner in = new Scanner(System.in);//不会检测输入越界的问题,但是注意整型上限,最好将变量定义为long长整型
        do{
            String line = in.nextLine();
            if( line==null || "".equals(line.toString())){
                flag = true;
            }else{
                list.add(line);
            }
        }while(!flag && N<20000);
        
        for(i=0;i<list.size()-1;i++){
            for(j=i+1;j<list.size();j++){
                String strA = list.get(i);
                String strB =  list.get(j);
                int result=edit_distance_Result(strA, strB);
                if(result <= 5){
                    count++;
                }
            }
            if(maxcount<count){
                maxcount = count;
            }
            count = 0;
        }
        System.out.println(++maxcount);
    }


    /**

     * 相似度比较

     * @param strA

     * @param strB

     * @return

     */

    public static int edit_distance_Result(String strA, String strB){

        String newStrA = removeSign(strA);

        String newStrB = removeSign(strB);

        int temp = Math.max(newStrA.length(), newStrB.length());

        int temp2 = compare(newStrA, newStrB);      //比较两个字符串,返回未匹配的字符串个数
        
        //System.out.println("temp2 == "+temp2);
        
        return (temp2);

    }
    
     private static int compare(String str, String target) {//
      int d[][]; // 矩阵
      int n = str.length();
      int m = target.length();
      int i; // 遍历str的
      int j; // 遍历target的
      char ch1; // str的

      char ch2; // target的

      int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
      if (n == 0) {
       return m;
      }

      if (m == 0) {
       return n;
      }

      d = new int[n + 1][m + 1];
      for (i = 0; i <= n; i++) { // 初始化第一列

          d[i][0] = i;

      }
      for (j = 0; j <= m; j++) { // 初始化第一行

       d[0][j] = j;

      }
      for (i = 1; i <= n; i++) {// 遍历str
       ch1 = str.charAt(i - 1);
       // 去匹配target
       for (j = 1; j <= m; j++){

        ch2 = target.charAt(j - 1);
        if (ch1 == ch2) {
         temp = 0;
        } else {

         temp = 1;
        }
        // 左边+1,上边+1, 左上角+temp取最小
        d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
       }
      }

      return d[n][m];

     }
    
     private static int min(int one, int two, int three) {
      return (one = one < two ? one : two) < three ? one : three;

     }


    private static String removeSign(String str) {

        StringBuffer sb = new StringBuffer();

        for (char item : str.toCharArray())

            if (charReg(item)){

                //System.out.println("--"+item);

                sb.append(item);

            }

        return sb.toString();

    }




    private static boolean charReg(char charValue) {

        return (charValue >= 0x4E00 && charValue <= 0X9FA5)

                || (charValue >= 'a' && charValue <= 'z')

                || (charValue >= 'A' && charValue <= 'Z')

                || (charValue >= '0' && charValue <= '9');

    }


    /*

    private static String longestCommonSubstring(String strA, String strB) {

        char[] chars_strA = strA.toCharArray();

        char[] chars_strB = strB.toCharArray();

        int m = chars_strA.length;

        int n = chars_strB.length;

        int[][] matrix = new int[m + 1][n + 1];

        for (int i = 1; i <= m; i++) {

            for (int j = 1; j <= n; j++) {

                if (chars_strA[i - 1] == chars_strB[j - 1])

                    matrix[i][j] = matrix[i - 1][j - 1] + 1;

                else

                    matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]);

            }

        }

        char[] result = new char[m+n+1];

        int currentIndex = result.length - 1;

        while (matrix[n].length != 0 && m>1 && n>1) {

            if (matrix[n] == matrix[n - 1])

                n--;

            else if (matrix[n].length == matrix[m - 1][n])

                m--;

            else {

                result[currentIndex] = chars_strA[m - 1];

                currentIndex--;

                n--;

                m--;

            }

        }

        return new String(result);

    }*/

}

这是作者提供的一个答案,但是未经过ms hihocoder.平台的测试,因为时间太短,来不及提交!可以输出结果,时间空间的效率还不高,欢迎大家一起讨论!






1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值