字符串匹配问题
来源:百度文库 编辑:神马文学网 时间:2024/04/29 09:42:54
题目:给定一字符串strOriginal(全英文小写字母组成), 并任意给一字符串strDesc, 编写函数实现: 判断strDesc是否为strOriginal的任意排序, 注意重复。 比如:给定字符串为adcbae,那么dacbea、cabeda都是匹配的,但是dacbe、cabedg不匹配 java代码实现如下: package others;import java.util.Arrays;
import java.util.Scanner; /**
*
* 用散列方式实现 *
*/public class TestStr {
private static boolean testStr(String strOriginal, String strDesc) {
int[] hashOriginal = new int[26];
int[] hashDesc = new int[26]; if (strOriginal.length() != strDesc.length()) {
return false;
} for (int index = 0; index < strOriginal.length(); ++index) {
int indexOriginal, indexDesc;
indexOriginal = strOriginal.charAt(index) - 'a';
hashOriginal[indexOriginal] += 1;
indexDesc = strDesc.charAt(index) - 'a';
hashDesc[indexDesc] += 1;
} return Arrays.equals(hashOriginal, hashDesc);
} public static void main(String[] args) {
System.out.print("请输入源字符串:");
Scanner in = new Scanner(System.in);
String str1 = (String) in.nextLine();
System.out.println();
System.out.print("请输入目标字符串:");
Scanner in1 = new Scanner(System.in);
String str2 = (String) in1.nextLine();
System.out.println();
System.out.println("源字符串为:" + str1);
System.out.println("目标字符串为:" + str2);
if (testStr(str1, str2)) {
System.out.println("匹配成功!");
} else {
System.out.println("匹配失败!");
}
}
}
import java.util.Scanner; /**
*
* 用散列方式实现 *
*/public class TestStr {
private static boolean testStr(String strOriginal, String strDesc) {
int[] hashOriginal = new int[26];
int[] hashDesc = new int[26]; if (strOriginal.length() != strDesc.length()) {
return false;
} for (int index = 0; index < strOriginal.length(); ++index) {
int indexOriginal, indexDesc;
indexOriginal = strOriginal.charAt(index) - 'a';
hashOriginal[indexOriginal] += 1;
indexDesc = strDesc.charAt(index) - 'a';
hashDesc[indexDesc] += 1;
} return Arrays.equals(hashOriginal, hashDesc);
} public static void main(String[] args) {
System.out.print("请输入源字符串:");
Scanner in = new Scanner(System.in);
String str1 = (String) in.nextLine();
System.out.println();
System.out.print("请输入目标字符串:");
Scanner in1 = new Scanner(System.in);
String str2 = (String) in1.nextLine();
System.out.println();
System.out.println("源字符串为:" + str1);
System.out.println("目标字符串为:" + str2);
if (testStr(str1, str2)) {
System.out.println("匹配成功!");
} else {
System.out.println("匹配失败!");
}
}
}
字符串匹配问题
KMP字符串模式匹配
KMP字符串匹配算法笔记
KMP字符串模式匹配zhuan
ORA_01861:文字与格式字符串不匹配
Java中使用正则表达式实现字符串匹配
Java中使用正则表达式实现字符串匹配_似虫非虫
Java中使用正则表达式实现字符串匹配
VB中字符串符号$的问题
c/c++程序之_KMP字符串模式匹配详解 - A_B_C_ABC欢迎你 - CSDN博客
KMP:在一个长字符串中匹配一个短子串的无回溯算法
KMP:在一个长字符串中匹配一个短子串的无回溯算法
关于正则表达式匹配空格的问题
Sybase12.5数据库安装,删除以及连接字符串问题
关于人力资源管理中人—岗匹配问题研究
昨天遇上一个字符串问题,今天又遇上不能调试的问题 - eazy ‘s life
symbian中文字符串显示问题的解决(转载) - bodybo的专栏 - CSDNBlo...
字符串操作
字符串操作
字符串处理
字符串分割
主 题: 字符串NULL的使用问题————本版面好像从未出现过,很难,请高手
前端总线频率(FSB)及DDR内存频率详解,彻底让你明白CPU总线带宽与内存带宽匹配问题!
字符串hash算法比较|字符串,hash,算法