《算法》第五章部分程序 part 1
▶ 书中第五章部分程序,包括在加上自己补充的代码,字母表类,字符串低位优先排序(桶排)
● 字母表类
package package01; import edu.princeton.cs.algs4.StdOut; public class class01
{
public static final class01 BINARY = new class01("01"); public static final class01 OCTAL = new class01("01234567"); public static final class01 DECIMAL = new class01("0123456789"); public static final class01 HEXADECIMAL = new class01("0123456789ABCDEF"); public static final class01 DNA = new class01("ACGT"); public static final class01 LOWERCASE = new class01("abcdefghijklmnopqrstuvwxyz"); public static final class01 UPPERCASE = new class01("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); public static final class01 PROTEIN = new class01("ACDEFGHIKLMNPQRSTVWY");// 蛋白质? public static final class01 BASE64 = new class01("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); public static final class01 ASCII = new class01(128); public static final class01 EXTENDED_ASCII = new class01(256); public static final class01 UNICODE16 = new class01(65536); private char[] alphabet; // 索引 -> 字母
private int[] inverse; // 字母 -> 索引
private final int R; // 字母表基数 public class01(String alpha) // 从字符串生成一个字母表
{
boolean[] unicode = new boolean[Character.MAX_VALUE]; // 检查输入字符串是否有重复字母
for (int i = 0; i < alpha.length(); i++)
{
char c = alpha.charAt(i);
if (unicode[c])
throw new IllegalArgumentException("\n<Constructor> repeated character = '" + c + "'.\n");
unicode[c] = true;
}
alphabet = alpha.toCharArray();
R = alpha.length();
inverse = new int[Character.MAX_VALUE];
for (int i = 0; i < inverse.length; i++)
inverse[i] = -1;
for (int c = 0; c < R; c++)
inverse[alphabet[c]] = c;
} private class01(int radix) // 从基数生成一个字母表
{
R = radix;
alphabet = new char[R];
inverse = new int[R];
for (int i = 0; i < R; i++) // 正反向都初始化为 0 ~ R-1
alphabet[i] = (char)i;
for (int i = 0; i < R; i++)
inverse[i] = i;
} public class01() // 默认构造扩展 aSCII
{
this(256);
} public boolean contains(char c)
{
return inverse[c] != -1;
} public int radix()
{
return R;
} public int lgR() // 表示字母表所需要的二进制位数
{
int lgR = 0;
for (int t = R - 1; t > 0; t <<= 1)
lgR++;
return lgR;
} public int toIndex(char c) // 字符转索引
{
if (c >= inverse.length || inverse[c] == -1)
throw new IllegalArgumentException("\n<toIndex> c >= inverse.length || inverse[c] == -1.\n");
return inverse[c];
} public int[] toIndices(String s) // 字符串转数组
{
char[] source = s.toCharArray();
int[] target = new int[s.length()];
for (int i = 0; i < source.length; i++)
target[i] = toIndex(source[i]);
return target;
} public char toChar(int index)
{
if (index < 0 || index >= R)
throw new IllegalArgumentException("\n<toChar> index < 0 || index >= R.\n");
return alphabet[index];
} public String toChars(int[] indices) // 数组转字符串
{
StringBuilder s = new StringBuilder(indices.length); // 使用 StringBuilder 类,防止平方级时间消耗
for (int i = 0; i < indices.length; i++)
s.append(toChar(indices[i]));
return s.toString();
} public static void main(String[] args)
{
int[] encoded1 = class01.BASE64.toIndices("NowIsTheTimeForAllGoodMen");
String decoded1 = class01.BASE64.toChars(encoded1);
StdOut.println(decoded1); int[] encoded2 = class01.DNA.toIndices("AACGAACGGTTTACCCCG");
String decoded2 = class01.DNA.toChars(encoded2);
StdOut.println(decoded2); int[] encoded3 = class01.DECIMAL.toIndices("01234567890123456789");
String decoded3 = class01.DECIMAL.toChars(encoded3);
StdOut.println(decoded3);
}
}
● 字符串低位优先排序(桶排)
package package01; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class class01
{
private static final int BITS_PER_BYTE = 8; private class01() { } public static void sort(String[] a, int w) // 每个字符串定长 w 的字符串数组排序
{
int n = a.length;
int R = 256; // 默认基数
String[] aux = new String[n]; for (int d = w - 1; d >= 0; d--) // 从最后一位起,每次循环处理一位
{
int[] count = new int[R + 1];
for (int i = 0; i < n; i++) // 桶计数,这里 count[0] 和 count [1] 暂时为 0
count[a[i].charAt(d) + 1]++;
for (int r = 0; r < R; r++) // 规约前缀和,完成后字母 c 在排序中的起始下标是 count[toIndex[c]],count[0] 固定为 0
count[r + 1] += count[r];
for (int i = 0; i < n; i++) // 向临时数组 aux 写入数据
aux[count[a[i].charAt(d)]++] = a[i];
for (int i = 0; i < n; i++) // 排序完成的数组写回原数组
a[i] = aux[i];
}
} public static void sort(int[] a) // 有符号数组排序,以一个 Byte 为键
{
final int BITS = 32; // int 类型占 256 Bit
final int R = 1 << BITS_PER_BYTE; // 每个 Byte 表示 0 ~ 255
final int MASK = R - 1; // 0xFF,用于只保留最低位的蒙版
final int w = BITS / BITS_PER_BYTE; // int 类型占 4 Byte int n = a.length;
int[] aux = new int[n];
for (int d = 0; d < w; d++)
{
int[] count = new int[R + 1];
for (int i = 0; i < n; i++)
{
int c = (a[i] >> BITS_PER_BYTE * d) & MASK; // 保留从右往左数第 d 个 Byte
count[c + 1]++;
}
for (int r = 0; r < R; r++)
count[r + 1] += count[r];
if (d == w - 1) // 符号位,0x00-0x7F 要排在 0x80-0xFF 的后面
{
int shift1 = count[R] - count[R / 2]; // 低半段(正值)移到后面
int shift2 = count[R / 2];
for (int r = 0; r < R / 2; r++)
count[r] += shift1;
for (int r = R / 2; r < R; r++) // 高半段(负值)移到前面
count[r] -= shift2;
}
for (int i = 0; i < n; i++)
{
int c = (a[i] >> BITS_PER_BYTE * d) & MASK;
aux[count[c]++] = a[i];
}
for (int i = 0; i < n; i++)
a[i] = aux[i];
}
} public static void main(String[] args)
{
String[] a = StdIn.readAllStrings();
int n = a.length; int w = a[0].length(); // 检查字符串是否定长
for (int i = 0; i < n; i++)
assert a[i].length() == w : "\n<main> Strings not fixed length.\n"; sort(a, w); for (int i = 0; i < n; i++)
StdOut.println(a[i]);
}
}
《算法》第五章部分程序 part 1的更多相关文章
- 《算法》第五章部分程序 part 3
▶ 书中第五章部分程序,包括在加上自己补充的代码,字符串高位优先排序(美国国旗排序) ● 美国国旗排序 package package01; import edu.princeton.cs.algs4 ...
- 《算法》第五章部分程序 part 8
▶ 书中第五章部分程序,包括在加上自己补充的代码,适用于基因序列的 2-Bit 压缩算法,行程长压缩算法,Huffman 压缩算法,LZW 压缩算法 ● 适用于基因序列的 2-Bit 压缩算法 pac ...
- 《算法》第五章部分程序 part 7
▶ 书中第五章部分程序,包括在加上自己补充的代码,字符串的二进制表示.十六进制表示.图形表示 ● 二进制表示 package package01; import edu.princeton.cs.al ...
- 《算法》第五章部分程序 part 6
▶ 书中第五章部分程序,包括在加上自己补充的代码,非确定性有穷自动机(NFA),grep 命令(利用 NFA 匹配) ● 非确定性有穷自动机(NFA) package package01; impor ...
- 《算法》第五章部分程序 part 5
▶ 书中第五章部分程序,包括在加上自己补充的代码,Knuth-Morris-Pratt 无回溯匹配,Boyer - Moore 无回溯匹配,Rabin - Karp 指纹匹配 ● Knuth-Morr ...
- 《算法》第五章部分程序 part 4
▶ 书中第五章部分程序,包括在加上自己补充的代码,Trie 树类,Trie 集合,三值搜索树(Ternary Search Trie) ● Trie 树类 package package01; imp ...
- 《算法》第五章部分程序 part 2
▶ 书中第五章部分程序,包括在加上自己补充的代码,字符串高位优先排序(计数 + 插排),(原地排序),(三路快排,与前面的三路归并排序相同) ● 计数 + 插排 package package01; ...
- Gradle 1.12用户指南翻译——第四十五章. 应用程序插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- 《算法》第一章部分程序 part 1
▶ 书中第一章部分程序,加上自己补充的代码,包括若干种二分搜索,寻找图上连通分量数的两种算法 ● 代码,二分搜索 package package01; import java.util.Arrays; ...
随机推荐
- position 分层固定在屏幕某位置
很多网站我们看到在屏幕右下角有一个,返回顶部,始终在那儿,还有些网站顶部菜单栏永远也是固定的不动,就是通过今天学习的position来做的. 在style中加入 positon:fixed;top 0 ...
- http修改443端口,http 强制跳转https
修改apache http/https 端口号 1.修改http的端口 打开$HTTPD_HOME/conf/httpd.conf文件,找到Listen,后面紧跟的是端口号,默认是80,把它修改为你想 ...
- AM二次开发中选择指定范围内的对象
使用Spatial可以快速选择指定范围内的对象 例如下面的代码可以选择所有在[0,0,0]-[10m,10m,10m]这个盒子之内的对象: 其中ElementsInBox还可以指定对象类型做进一步筛选 ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- MIME Types - Complete List
Suffixes applicable Media type and subtype(s) .3dm x-world/x-3dmf .3dmf x-world/x-3dmf .a applicatio ...
- bzoj4980: 第一题
Description 神犇xzyo听说sl很弱,于是出了一题来虐一虐sl.一个长度为2n(可能有前缀0)的非负整数x是good的,当且仅当 存在两个长度为n(可能有前缀0)的非负整数a.b满足a+b ...
- php 安装 phpredis 扩展
1. git clone https://github.com/nicolasff/phpredis2. 首先git clone 项目到本地,切换到phpredis目录下 phpize ./confi ...
- spring4.0之六:Generic Qualifier(泛型限定)
Spring 4.0已经发布RELEASE版本,不仅支持Java8,而且向下兼容到JavaSE6/JavaEE6,并移出了相关废弃类,新添加如Java8的支持.Groovy式Bean定义DSL.对核心 ...
- [转]Windows7:Visual Studio 2008试用版的评估期已经结束解决方法
原文来自:http://blog.sina.com.cn/s/blog_6b1815080100y5z3.html 以前在Windows2003碰到这个问题时,都是到"控制面板→添加 ...
- [C#][Quartz]帮助类
本文来自:http://www.cnblogs.com/pengze0902/p/6128558.html /// <summary> /// 任务处理帮助类 /// </summa ...