华为机试-公共字串计算
题目描述
题目标题:
计算两个字符串的最大公共字串的长度,字符不区分大小写
详细描述:
接口说明
原型:
int getCommonStrLength(char * pFirstStr, char * pSecondStr);
输入参数:
char * pFirstStr //第一个字符串
char * pSecondStr//第二个字符串
输入描述:
输入两个字符串
输出描述:
输出一个整数
输入例子:
asdfas werasdfaswer
输出例子:
6
效率高的Java程序实现:
- #include <iostream>
- #include <vector>
- #include <string>
- #include <fstream>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int main()
- {
- char s1[512],s2[512];
- int i,j,count=0;
- int maxA=0;
- while(cin.getline(s1,512),cin.getline(s2,512))
- {
- int length1=strlen(s1);
- int length2=strlen(s2);
- int k1,k2;
- count=0;
- maxA=0;
- for(i=0; i<length1; i++)
- {
- for(j=0; j<length2; j++)
- {
- k1=i;
- k2=j;
- count=0;
- while(s1[k1]==s2[k2] && s1[k1]!='\0' && s2[k2]!='\0')
- {
- k1++;
- k2++;
- count++;
- }
- if(count>maxA)
- {
- maxA=count;
- count=0;
- }
- }
- }
- cout<<maxA<<endl;
- }
- return 0;
- }
效率高的Java程序实现(动态规划):
- import java.util.Scanner;
- public class Main{
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- String str1 = "";
- String str2 = "";
- while(sc.hasNext()){
- str1 = sc.next();
- str2 = sc.next();
- str1=str1.toLowerCase() ;
- str2=str2.toLowerCase();
- System.out.println(getCommonStrLength(str1, str2));
- }
- }
- public static int getCommonStrLength(String str1, String str2){
- int len1 = str1.length();
- int len2 = str2.length();
- int[][] dp = new int[len1+1][len2+1];
- for(int i=0;i<=len1;i++){
- for(int j=0;j<=len2;j++){
- dp[i][j] = 0;
- }
- }
- for(int i=1;i<=len1;i++){
- for(int j=1;j<=len2;j++){
- if(str1.charAt(i-1) == str2.charAt(j-1)){
- dp[i][j] = dp[i-1][j-1] + 1;
- }else{
- dp[i][j] = 0; //区别在这儿
- }
- }
- }
- int max = 0;
- for(int i=0;i<=len1;i++){
- for(int j=0;j<=len2;j++){
- if(max < dp[i][j])
- max = dp[i][j];
- }
- }
- return max;
- }
- }
效率低的Java程序实现:
- import java.util.Scanner;
- /**
- * 华为机试公共字串计算
- *
- * @author LiJian 传统方法是对短的字符串从头进行遍历,选择不同的起点来进行遍历找到公共字串
- *
- * 我想的是利用字符串的contain方法来寻找字串,效率会略微高点
- */
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while (sc.hasNext()) {
- String pFirstStr = sc.next();
- String pSecondStr = sc.next();
- pFirstStr=pFirstStr.toLowerCase();
- pSecondStr=pSecondStr.toLowerCase();
- int num = pFirstStr.length() > pSecondStr.length() ? getCommonStrLength(pFirstStr, pSecondStr)
- : getCommonStrLength(pSecondStr, pFirstStr);
- System.out.println(num);
- }
- }
- private static int getCommonStrLength(String pFirstStr, String pSecondStr) {
- int start = 0;
- int end = pSecondStr.length() - 1;
- int max = 0;
- for (; start < pSecondStr.length(); start++) {
- if (max > pSecondStr.substring(start).length()) {
- break;
- }
- if (pFirstStr.contains(pSecondStr.substring(start))) {
- max = pSecondStr.substring(start).length();
- continue;
- }
- for (int j = end; j > start; j--) {
- if (j - start <= max) {
- break;
- }
- if (pFirstStr.contains(pSecondStr.substring(start, j))) {
- max = j - start;
- break;
- }
- }
- }
- return max;
- }
- }
posted on 2017-06-29 19:43 WenjieWangFlyToWorld 阅读(482) 评论(0) 编辑 收藏 举报