zoukankan      html  css  js  c++  java
  • 不借助编辑器自带的代码高亮工具(Syntaxhighlighte),生成完美格式的语法高亮代码

    请看下面的代码:

    public class KMP{
    
            public static void main(String args []){
                    testRollBackArray();
                    testKMPSearchMatch();
                    testKMPSearchNoMatch();
            }
    
    
        /**
         * each value of array rollback means: when source[i] mismatch pattern[i],
         * KMP will restart match process form rollback[j] of pattern with
         * source[i]. And if rollback[i] == -1, it means the current source[i] will
         * never match pattern. then i should be added by 1 and j should be set to
         * 0, which means restart match process from source[i+1] with pattern from
         * pattern[0].
         * 
         * @param pattern
         * @return
         */
        private static int[] getRollbackArray(char[] pattern) {
            int[] rollback = new int[pattern.length];
            for (int i = 0; i < pattern.length; i++) {
                rollback[i] = 0;
            }
            rollback[0] = -1;
            for (int i = 1; i < rollback.length; i++) {
                char prevChar = pattern[i - 1];
                int prevRollback = i - 1;
                while (prevRollback >= 0) {
                    int previousRollBackIdx = rollback[prevRollback];
                    if ((previousRollBackIdx == -1)
                            || (prevChar == pattern[previousRollBackIdx])) {
                        rollback[i] = previousRollBackIdx + 1;
                        break;
                    } else {
                        prevRollback = rollback[prevRollback];
                    }
                }
            }
            return rollback;
        }
    
        /**
         * search pattern chars in source chars.
         * 
         * @param source
         * @param pattern
         * @return
         */
        public static int searchKMP(char[] source, char[] pattern) {
            // validation
            if (source == null || source.length == 0 || pattern == null
                    || pattern.length == 0) {
                return -1;
            }
    
            // get the rollback array.
            int[] rollback = getRollbackArray(pattern);
    
            // incremental index of pattern. pointing the char to compare with.
            int currMatch = 0;
            int len = pattern.length;
            // i point the char to compare with
            for (int i = 0; i < source.length;) {
                // if current char match
                if ((currMatch == -1) || (source[i] == pattern[currMatch])) {
                    /*
                     * then each of the indexes adding by one, moving to the next
                     * char for comparation. notice that if currMatch is -1, it
                     * means the first char in pattern can not be matched. so i add
                     * by one to move on. and currMatch add by one so its value is
                     * 0.
                     */
                    i++;
                    currMatch++;
                    /*
                     * if reaches the end of pattern, then match success, return the
                     * index of first matched char.
                     */
                    if (currMatch == len) {
                        return i - len;
                    }
                } else {
                    /*
                     * if current char mismatch, then rollback the next char to
                     * compare in pattern.
                     */
                    currMatch = rollback[currMatch];
                }
            }
            return -1;
        }
    
            public void testRollBackArray() {
                    int[] expectedRollback = new int[] { -1, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0,
                                    0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0 };
                    int[] rollback = getRollbackArray("PARTICIPATE IN PARACHUTE"
                                    .toCharArray());
                    Assert.assertArrayEquals("Rollback array compare failed to match!",
                                    expectedRollback, rollback);
            }
    
            public void testKMPSearchMatch() {
                    int matchIndex = searchKMP(
                                    "aaaaaababacbaslierjalsdzmflkasjf".toCharArray(),
                                    "ababacb".toCharArray());
                    Assert.assertEquals(5, matchIndex);
    
                    matchIndex = searchKMP(
                                    "aaaaaababacbaslierjalsdzmflkasjf".toCharArray(),
                                    "aaaaaababacbaslierjalsdzmflkasjf".toCharArray());
                    Assert.assertEquals(0, matchIndex);
            }
    
            public void testKMPSearchNoMatch() {
                    int matchIndex = searchKMP("ABCABCDABABCDABCDABDE".toCharArray(),
                                    "hjABCDABD".toCharArray());
                    Assert.assertEquals(-1, matchIndex);
    
            }
    
    
    }
    

    我这里实现语法高亮,直接是由HTML+CSS完成的。一般的编辑器都支持HTML编辑,邮件也可以哦。

    • 作者:zollty(接收技术咨询和工作机会)
    • 出处:http://www.cnblogs.com/zollty/
    • 本文版权归作者和博客园共有,欢迎转载,但请在文章页面明显位置给出原文连接,否则视为侵权。
    • 关于博主,85后程序员,擅长复杂分布式系统架构,中间件设计开发,关注效率/性能/稳定性。
    • 对于自己,博文只是总结。在总结的过程发现问题,解决问题。
    • 对于他人,在此过程如果还能附带帮助他人,那就再好不过了。
    • 感谢您的阅读。如果文章对您有用,那么请轻轻点个赞,以资鼓励。
  • 相关阅读:
    php中的form表单
    http
    PHP做的简单计算器
    用Js写贪吃蛇
    ajax简单操作,验证用户名是否可以
    关于ARM PC值
    阅读脚本控制pwm代码
    编译选项
    关于机器码、原码、反码、补码
    makefile学习之函数
  • 原文地址:https://www.cnblogs.com/zollty/p/2879237.html
Copyright © 2011-2022 走看看