1. 程式人生 > >c語言查詢兩個字串中的公共子串

c語言查詢兩個字串中的公共子串

程式碼如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void getCommon(char str1[], char str2[], char * str3);
int stringLength(char * str);

void main(){
	char str1[30];
	char str2[30];
	char str3[30];
	fgets(str1, sizeof(str1), stdin);
	fgets(str2, sizeof(str2), stdin);
	getCommon(str1, str2, str3);
	printf("%s\n", str3);
}
int stringLength(char * str){
	int len = 0;
	while (*str != '\0'){
		len++;
		str++;
	}
	return len;
}
void getCommon(char str1[], char str2[], char * str3){
	int len1, len2;
	int i, j, k;
	int max = 0;
	int start = -1;

	len1 = stringLength(str1);
	len2 = stringLength(str2);
	for (i = 0; i < len1; i++){
		for (j = 0; j < len2; j++){
			if (str1[i] == str2[j]){
				for (k = 0; (str1[i + k] == str2[j + k] && str1[i + k] != '\0'); k++);
				if (max < k){
					max = k;
					start = i;
				}
			}
		}
	}

	if (start == -1){
		str3[0] = '\0';;
	}
	else{
		memcpy(str3, &str1[start], max);
		str3[max] = '\0';
	}
}

相關推薦

c語言查詢字串公共

程式碼如下:#include <stdio.h> #include <string.h> #include <stdlib.h> void getCommon(ch

c語言實現陣列的內容進行交換。(陣列一樣大)

方法一:建立新的陣列。 #include<stdio.h> #include<stdlib.h> int main() { int arr1[5] = { 1, 2, 3, 4, 5, }; int arr2[5] = { 0, 6, 7, 8, 9, }

Java學習 查詢字串的最大子

public static String getMaxSubString(String str1,String str2) { String max = null,min = null; if(str1.length()>str2.length())     

查詢字串的相同的字元位置

class Solution { public:     vector<int> findAnagrams(string s, string p) {      vector<int>ve1(256,0);            vector<int>ve2(256,0);

c語言比較字串是否相等strcmp

c語言提供了幾個標準庫函式,可以比較兩個字串是否相同。以下是用strcmp()函式比較字串的一個例子: 個人網站:www.ctrlqun.com #include <stdio.h> #include <string.h> int main(vo

c語言 判斷字串是否相等

#include <stdio.h> #include <string.h> int fun(char *a,char *b) {     int i,flag=0;     f

codeforces D.神祕的犯罪 + n字串公共+技巧

在m組數,每組有n個數(數的範圍1-n)中,找到某些序列 使它是每組數的一個公共子序列,問這樣的某些序列的個數? 1≤n≤100000 , 1≤m≤10 公共子串:1 2 3 23一共4個。 思考:我當時想的是暴力搜尋一遍,找到1,2序列的公共子串(長度>1)在去3~n序列搜尋

C程式設計——程式設計實現查詢字串的最大公共2.0

1、 題目:程式設計實現查詢兩個字串的最大公共子串 示例:“aocdfe"和"pmcdfa"最大公共子串為"cdf” 注:最大公共子串有不止一個時,只輸出第一個最大子串 利用斐波那契數列數的思想 **2、**程式 #include <stdio.h> #include &

C程式設計——程式設計實現查詢字串的最大公共1.0

1、 題目:程式設計實現查詢兩個字串的最大公共子串 示例:“aocdfe"和"pmcdfa"最大公共子串為"cdf” 注:最大公共子串有不止一個時,只輸出第一個最大子串 **2、**程式 #include <stdio.h> #include <string.h>

[OJ-java] 查詢字串a,b的最長公共

目的 記錄自己做過的有價值的程式碼 題目 如題,即查詢兩個字串stringA和stringB中的最長公共子串。成功返回最大公共子串,不成功返回null。 程式碼 public static String iQueryMaxCommString(Str

查詢字串a,b的最長公共

#include<iostream> #include<string> using namespace std; string iQueryMaxCommString(string s1, string s2); int main(){ s

C語言實現 int(32位)整數m和n的二進位制表達,有多少個位(bit)不同?

輸入例子: 1999 2299 輸出例子:7 int main() { int a = 0; int b = 0; int num = 0; int count = 0; printf("請輸入兩個整數:"); scanf("%d%d",&a,&b); n

程式設計實現查詢字串的最大公共

#include <stdio.h> #include <string.h> //程式設計實現查詢兩個字串的最大公共子串 //示例:"aocdfe"和"pmcdfa"最大公共子串為"cdf" void MyPub(char *str1, char *str2) {

702. 連線字串的不同字元的C++解法

這道題的兩個需要注意的細節在於: 1.要分辨是字串內重複還是字串間重複(S1中有n個a都是允許的,但是S1和S2都有a不可以); 2.要按照字母在字串中出現的先後順序連線,所以只用hash表列印是不行的。 class Solution { public: string concatenet

找出字串最大的公共(java實現)

import java.util.HashSet; import java.util.Set; public class Test { public static void main(String[] args) { String

C語言int(32位)整數m和n的二進位制表達,有多少個位(bit)不同

根據異或我們可以知道,兩個數字的二進位制位按位異或,相同為0,相異為1。 因此我們可以通過將兩個數字按位異或,並計算該異或結果中二進位制位中1的個數,即可知道有多少個位元位不同。 int count(int a, int b) { int m = a ^ b; // 兩個數按位異或,對應不

字串操作】 尋找字串的 最大公共

*題目描述:請編寫一個函式,求2個字串的最長公共子串,n<20,字元長度不超過255.       例如有2個字串為:       Name some local bus.       local bus is high speed I/O bus close to

135、程式設計實現:找出字串最大公共字串,如"abccade","dgcadde"的最大子為 "cad"

35、程式設計實現:找出兩個字串中最大公共子字串,如"abccade","dgcadde"的最大子串為 "cad" /* 35、程式設計實現:找出兩個字串中最大公共子字串,如"abccade","dgcadde"的最大子串為 "cad" 不同於56的最長公共子串 DP題

LR中用C語言比較字符變量

nat [] 變量 end put tar 字符串 init rmi 以下腳本,定義兩個一樣的字符數組,對比後,打印出result的值: Action() { int result; char string1[] = "We can see the st

C語言】統計一個字串字母、數字、空格及其它字元的數量

統計一個字串中字母、數字、空格及其它字元的數量 解法1: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> void Count(con