笔试中遇到的问题

笔试中遇到的问题


KMP算法

KMP算法是一种字符串匹配算法。匹配过程详见wiki或者阮一峰的博客

public class KMP {
    private final int R;       // the radix
    private int[][] dfa;       // the KMP automoton

    private char[] pattern;    // either the character array for the pattern
    private String pat;        // or the pattern string

    /**
     * Preprocesses the pattern string.
     *
     * @param pat the pattern string
     */
    public KMP(String pat) {
        this.R = 256;
        this.pat = pat;

        // build DFA from pattern
        int m = pat.length();
        dfa = new int[R][m]; 
        dfa[pat.charAt(0)][0] = 1; 
        for (int x = 0, j = 1; j < m; j++) {
            for (int c = 0; c < R; c++) 
                dfa[c][j] = dfa[c][x];     // Copy mismatch cases. 
            dfa[pat.charAt(j)][j] = j+1;   // Set match case. 
            x = dfa[pat.charAt(j)][x];     // Update restart state. 
        } 
    } 

    /**
     * Preprocesses the pattern string.
     *
     * @param pattern the pattern string
     * @param R the alphabet size
     */
    public KMP(char[] pattern, int R) {
        this.R = R;
        this.pattern = new char[pattern.length];
        for (int j = 0; j < pattern.length; j++)
            this.pattern[j] = pattern[j];

        // build DFA from pattern
        int m = pattern.length;
        dfa = new int[R][m]; 
        dfa[pattern[0]][0] = 1; 
        for (int x = 0, j = 1; j < m; j++) {
            for (int c = 0; c < R; c++) 
                dfa[c][j] = dfa[c][x];     // Copy mismatch cases. 
            dfa[pattern[j]][j] = j+1;      // Set match case. 
            x = dfa[pattern[j]][x];        // Update restart state. 
        } 
    } 

    /**
     * Returns the index of the first occurrrence of the pattern string
     * in the text string.
     *
     * @param  txt the text string
     * @return the index of the first occurrence of the pattern string
     *         in the text string; N if no such match
     */
    public int search(String txt) {

        // simulate operation of DFA on text
        int m = pat.length();
        int n = txt.length();
        int i, j;
        for (i = 0, j = 0; i < n && j < m; i++) {
            j = dfa[txt.charAt(i)][j];
        }
        if (j == m) return i - m;    // found
        return n;                    // not found
    }

    /**
     * Returns the index of the first occurrrence of the pattern string
     * in the text string.
     *
     * @param  text the text string
     * @return the index of the first occurrence of the pattern string
     *         in the text string; N if no such match
     */
    public int search(char[] text) {

        // simulate operation of DFA on text
        int m = pattern.length;
        int n = text.length;
        int i, j;
        for (i = 0, j = 0; i < n && j < m; i++) {
            j = dfa[text[i]][j];
        }
        if (j == m) return i - m;    // found
        return n;                    // not found
    }


    /** 
     * Takes a pattern string and an input string as command-line arguments;
     * searches for the pattern string in the text string; and prints
     * the first occurrence of the pattern string in the text string.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        String pat = args[0];
        String txt = args[1];
        char[] pattern = pat.toCharArray();
        char[] text    = txt.toCharArray();

        KMP kmp1 = new KMP(pat);
        int offset1 = kmp1.search(txt);

        KMP kmp2 = new KMP(pattern, 256);
        int offset2 = kmp2.search(text);

        // print results
        System.out.println("text:    " + txt);

        System.out.print("pattern: ");
        for (int i = 0; i < offset1; i++)
            System.out.print(" ");
        System.out.println(pat);

        System.out.print("pattern: ");
        for (int i = 0; i < offset2; i++)
            System.out.print(" ");
        System.out.println(pat);
    }
}

二分查找

    /**
     * 二分查找
     * @param sorted
     * @param target
     * @return
     */
    public int binarySearch(int[] sorted, int target) {
        int low = 0;
        int high = sorted.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (sorted[mid] == target) {
                return mid;
            }
            if (sorted[mid] > target) {
                high = mid - 1;
            }
            if (sorted[mid] < target) {
                low = mid + 1;
            }
        }
        return -1;
    }

例如:Array:2 5 8 12 16 23 38 56 72 91,Target:23。则pivot分别是:16 -> 56

CSMA/CD

Carrier-sense multiple access with collision detection (CSMA/CD) is a media access control method used most notably in early Ethernet technology for local area networking.

一项局域网技术。

循环冗余算法

一种数据校验算法,英文缩写是CRC。

局域网技术

  • CSMA/CD
  • Token ring
  • AppleTalk
  • ATM

关于IP地址的分配

线程资源

共享的有:

  • 进程代码段
  • 进程的公有数据
  • 进程打开的文件描述符
  • 信号的处理器
  • 进程的当前目录和进程用户ID与进程组ID

线程私有资源:

  • 线程ID
  • 寄存器组的值
  • 线程的堆栈
  • 错误返回码
  • 线程的信号屏蔽码
  • 线程的优先级

广度优先和深度优先遍历

排列组合

排列是有序的;组合是无序的。

  1. 特殊元素(位置)优先安排法
    例如:用0,2,3,4,5五个数字,组合没有重复数字的三位数,问偶数有几个?(30)
    :这里的0不能在第一位,所以是特殊元素,先考虑。

  2. 总体淘汰法(排除法)

  3. 相邻捆绑法
    解决相邻问题。

  4. 不相邻插空法
    解决不相邻问题。

  5. 顺序固定用除法
    例如:6个人排队,甲乙丙三人按甲乙丙顺序排列的方法有多少种?
    A66 / A33 = 120

  6. 分排问题直排法
    多排问题转一排。

  7. 无脑实验法

  8. 隔板法
    例如:方程a + b + c + d = 12有多少组正整数解?
    :转化为12个球分4份的问题。

  9. 对偶法
    “至多”“至少”情况;比如至少一个,那他的对立面就是都没有。

数组类型

一个数组初始化为什么类型,则接下来存入的元素只能是该类型或子类。

public class TestArrayObject {

    public static void main(String[] args) {
        A[] as = new B[10];
        as[0] = new C(); // would be error
        as[1] = new BB();
        as[2] = new A(); // would be error
    }

}

class A {}
class B extends A {}
class C extends A {}
class BB extends B {}

SQL组合索引

ALTER TABLE myIndex ADD INDEX name_city_age(vc_Name(10),vc_City,i_Age);

相当于建立了:

vc_Name,vc_City,i_Age
vc_Name,vc_City
vc_Name

猜你喜欢

转载自blog.csdn.net/m0_37867653/article/details/82532298
今日推荐