大话数据结构读书笔记(五)-串
来源:互联网 发布:淘宝主播的东西可靠吗 编辑:程序博客网 时间:2024/04/30 13:06
五、串
串:定义是由零个或者多个字符组成的有限序列又叫字符串。串长,字符串中字符个数。空格串只含有空格,空串不含有任何字符。串的比较是比较对应位置上字符大小。
串的存储结构:顺序存储和链式存储(数组)。链式存储链式线性表。
字符串基本操作
package day01to05;public class stringoper { public static void method(){ String s="Helloword Java"; display("原字符串为:"+s); display(s.toUpperCase());//转换大写 display(s.toLowerCase()); //转换小写 display(s.trim()); //去空格 String s1="abc"; String s2="aaa"; display(s1.compareTo(s2)); //比较 } //子串获取字符串中的一部分 public static void method_sub(){ String s="abcdefghijklmnopqrstuvwxyz"; display("原字符串为"); display(s); String s1=s.substring(9); //从指定位置到结尾。如果角标不存在,则会出现字符串角标越界异常。 display("获取的子串s1为:"); display(s1); String s2=s.substring(7,20); //包含头,不包含尾。 获取整个字符串:s.substring(0,s.length()); display("获取的子串s2为:"); display(s2); } //切割 public static void method_split(){ String s="zhangsan,lisi"; String[] arr=s.split("a"); display("原字符串为:"); display(s); display("切割后的字符串为:"); for(int x=0;x<arr.length;x++) { System.out.print(arr[x]+" "); } System.out.println(); } //判断 public static void method_is(){ String str="ArrayDemo.java"; String str1="arraydemo.java"; //判断文件名称是否以Array开头 display(str.startsWith("Array")); //判断文件名称是否是以.java结尾 display(str.endsWith(".java")); //判断文件名称中是否包含Demo display(str.contains("Demo")); //判断两个文件名是否相同(区分大小写) display(str.equals(str1)); //判断两个文件名是否相同(不区分大小写) display(str.equalsIgnoreCase(str1)); } //获取 public static void method_get(){ String str="abcde"; display("字符串为:"+str); //长度 display("字符串的长度为:"+str.length()); //根据索引获取字符 display("角标为四的位置上的字符为:"+str.charAt(4));//当访问到字符串中不存在的角标时,会发生 StringIndexOutOfBoundsException异常 //根据字符获取索引 display("从角标为3的位置开始往后索引 a 出现的位置为:"+str.indexOf('a',3));//如果没有找到返回-1 //反向索引一个字符出现的位置 display("从字符串右面开始索引第一个a出现的位置为:"+str.lastIndexOf("a")); } //转换 public static void method_trans(){ char[] arr={'a','b','c','d','e','f','g','h'}; String str="jkasdhavsgjv"; char[] a=str.toCharArray(); //字符串操作 System.out.print("将字符串转换为字符数组为:["); for(int x=0;x<a.length;x++) { if(x<a.length-1) System.out.print(a[x]+","); else System.out.print("]"); } System.out.println(); //字符数组操作 System.out.print("字符数组为:["); for(int x=0;x<arr.length;x++) { if(x<arr.length-1) System.out.print(arr[x]+","); else System.out.print("]"); } System.out.println(); String s=new String(arr); display("转换成字符串为:"+s); //获取从角标为1的位置的字符开始三个字符 String s1=new String(arr,1,3); display("从角标为1的位置的字符开始三个字符组成的字符串为:"+s1); } //替换 public static void method_replace(){ String s=" hello java "; String s1=s.replace('a', 'n'); //如果要替换的字符不存在,则返回的还是原字符串 display("原来的字符串为:"+s); display("替换字符后的字符串为:"+s1); } //主函数 public static void main(String[] args) { method_get(); method_is(); method_trans(); method_replace(); method_split(); method_sub(); method(); } public static void display(Object obj){ System.out.println(obj); } }字符串匹配:朴素模式匹配从目标字符串初始位置开始,依次分别与Pattern的各个位置的字符比较,如相同,比较下一个位置的字符直至完全匹配;如果不同则跳到目标字符串下一位置继续如此与Pattern比较,直至找到匹配字符串并返回其位置。时间复杂度最坏为O(mn),效率很低。
public class stringoper { public static void main(String[] args) { String s="abcdefg"; String str="bc5d"; System.out.println(Index(s, str)); } public static int Index(String s, String pattern) { int index = -1; boolean match = true; for (int i = 0, len = s.length() - pattern.length(); i <= len; i++) { match = true; for (int j = 0; j < pattern.length(); j++) { if (s.charAt(i + j) != pattern.charAt(j)) { match = false; break; } } if (match) { index = i; break; } } return index; } }2、KMP算法:KMP算法是一种改进的字符串匹配算法,关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。在BF算法的基础上使用next函数来找出下一次目标函数与Pattern比较的位置,因为BF算法每次移动一位的比较是冗余的,KMP利用Pattern字符重复的特性来排除不必要的比较,从而可以每次移动n位来排除冗余。对于Next函数近似接近O(m),KMP算法的时间复杂度为O(n),所以整个算法的时间复杂度为O(n+m)。
public class stringoper { public static void main(String[] args) { stringoper.getNext("abcdef"); System.out.println(stringoper.indexKMP("abcadef", "abc",0));} public static int[] getNext(String T){ int[] next = new int[T.length()]; int i = 0; int j = -1; next[i] = j; while(i<T.length()-1){ if(j==-1 || T.charAt(i)==T.charAt(j)){ i++; j++; next[i] = j; }else { j = next[j];//若字符不相等,则j值进行回溯。 } } for (int k = 0; k < next.length; k++) { System.out.print(next[k]); } System.out.println(); return next;}public static int indexKMP(String S,String T,int pos){ int i = pos;//表示从主串中第几个字符串开始匹配 int j = 0;//表示从模式串中第几个字符串开始 int[] next = getNext(T); while(i<S.length()&&j<T.length()){ if (j == -1 || S.charAt(i) == T.charAt(j)) { i++; j++; } else {//重新开始匹配 j = next[j];//j退回到合适的位置,i值不变 } } if (j>=T.length()) { return i-T.length(); }else { return 0; } }}
0 0
- 大话数据结构读书笔记系列(五)串
- 大话数据结构读书笔记(五)-串
- 《大话数据结构》读书笔记(五)
- 大话数据结构(五)串
- 大话数据结构 第五章 串 (读书笔记)
- 大话数据结构读书笔记 3---串
- 《大话数据结构》读书笔记(一)
- 《大话数据结构》读书笔记(二)
- 《大话数据结构》读书笔记(三)
- 《大话数据结构》读书笔记(四)
- 《大话数据结构》读书笔记(1)
- 《大话数据结构》读书笔记(2)
- 《大话数据结构》读书笔记(一)
- 《大话数据结构》读书笔记(二)
- 《大话数据结构》读书笔记(三)
- 《大话数据结构》读书笔记之串和源码
- 大话数据结构——读书笔记(2)
- 【读书笔记】大话数据结构之 栈(1)
- hibernate零散知识点
- photoshop第十章:抠图技巧以及示例
- hibernate多对多
- jVMgc-6 吞吐量收集器
- JVM了解
- 大话数据结构读书笔记(五)-串
- set集合如何遍历
- Codeforces Round #398 (Div. 2) C. Garland 后序遍历树
- 如何用photoshop把一张图片分割成几张图片呢?
- HelloWorld
- mybatis案例
- POJ 3562 “Roman” corridor 英文少
- Java编程题练习2017-02-18
- 2016年度总结