java:字符串
来源:互联网 发布:中国网络诗歌网 编辑:程序博客网 时间:2024/04/29 22:30
字符串基本类
- String, StringBuffer,StringBuilder
- String是不可变类
- StringBuffer是可变,通过方法:append+insert+reverse+setCharAt+setLength
正则表达式
字符串的排序
- LSD(低位优先检查字符串)从右到左
- MSD(高位优先检查字符串)从左到右
索引计数法
- 适用于小整数
lsd
public class LSD { private static final int BITS_PER_BYTE = 8; // do not instantiate private LSD() { } /** * Rearranges the array of W-character strings in ascending order. * * @param a the array to be sorted * @param W the number of characters per string */ public static void sort(String[] a, int W) { int N = a.length; int R = 256; // extend ASCII alphabet size String[] aux = new String[N]; for (int d = W-1; d >= 0; d--) { // sort by key-indexed counting on dth character // compute frequency counts int[] count = new int[R+1]; for (int i = 0; i < N; i++) count[a[i].charAt(d) + 1]++; // compute cumulates for (int r = 0; r < R; r++) count[r+1] += count[r]; // move data for (int i = 0; i < N; i++) aux[count[a[i].charAt(d)]++] = a[i]; // copy back for (int i = 0; i < N; i++) a[i] = aux[i]; } }
msd
import java.util.*; /* * 1.频数统计 * 2.将频数转换为索引 * 3.数据分类 * 4.回写 * * * MSD算法 * */class Solution{ private static final int BITS_PER_BYTE=8; private static final int BITS_PER_INT=32; private static final int R=256; private static final int CUTOFF=15; //插入排序的切换阈值 private Solution(){} public static void sort(String[] a){ int N=a.length; String[] aux=new String[N]; sort(a,0,N-1,0,aux); } // dth character of s, -1 if d=length of string private static int charAt(String s,int d){ assert d>=0 &&d<=s.length(); if(d==s.length()) return -1; return s.charAt(d); //返回第d个字符 } // 以第d个字符为键将a[lo]到a[hi]排序 private static void sort(String[] a,int lo,int hi,int d,String[] aux){ // 分割成子串排序 if(hi<=lo+CUTOFF){ insertion(a,lo,hi,d); return; } //计算频数 int[] count=new int[R+2]; for(int i=lo; i<=hi;i++){ int c=charAt(a[i],d); count[c+2]++; } //转换为索引 for(int r=0;r<R+1;r++) count[r+1]+=count[r]; //数据分类 for(int i=lo;i<=hi;i++){ int c=charAt(a[i],d); aux[count[c+1]++]=a[i]; } //copy back for(int i=lo;i<=hi;i++){ a[i]=aux[i-lo]; } //recursively sort for each character (excludes sentinel-1) for(int r=0;r<R;r++) sort(a,lo+count[r],lo+count[r+1]-1,d+1,aux); } //insertion sort a[lo..hi] starting at dth character private static void insertion(String[] a,int lo,int hi,int d){ for(int i=lo;i<=hi;i++) for(int j=i;j>lo && less(a[j],a[j-1],d);j--){ exch(a,j,j-1); } }}
字符串的查找
暴力算法
- 算法
复杂度:O(m*n)
import java.util.*;/* * i:跟踪文本 * j:跟踪 模式 * */class Solution{ public static int search(String pat, String txt){ int M=pat.length(); int N=txt.length(); for(int i=0;i<=N-M;i++){ int j; for( j=0;j<M;j++) if(txt.charAt(i+j)!=pat.charAt(j)) break; if(j==M) return i; // 找到匹配 } return N; //为找到匹配 }}
KMP
http://blog.csdn.net/yutianzuijin/article/details/11954939/
0 0
- java字符串--修改字符串
- java 字符串,字符串缓冲区
- java 字符串,字符串缓冲区
- java字符串
- Java字符串
- Java字符串
- Java字符串
- java字符串
- JAVA 字符串
- Java 字符串
- java-字符串
- Java - 字符串
- java 字符串
- java字符串
- java字符串
- java字符串
- java---字符串
- java字符串
- Tsinsen A1113 骑士
- JAVA里一直想实验的几个小问题
- 搭建简单JavaWeb测试环境搭建原因
- 10013---Trail ~ Catalogs
- 返回数据给上一个活动
- java:字符串
- 算法训练 Torry的困惑(基本型)
- Linux 下安装配置 JDK1.7
- 二叉树的建立
- servlet笔记
- 8259A使用摘要
- error: expected constructor, destructor, or type conversion before '(' token问题
- Scala map() vs map{}
- CodeForces 627D Preorder Test(树形DP+二分)