Created
September 15, 2018 05:36
-
-
Save completejavascript/64addc7a563a0c926788de6261cfca1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
const int MAX = 505; | |
char SB[MAX], SE[MAX]; // Xâu đích S, xâu đầu SB và xâu cuối SE | |
int LengB, LengE; // Độ dài tương ứng của SB và SE. | |
/* | |
* Tính độ dài xâu | |
* @PARAM: str : xâu cần tính độ dài | |
* RETURN: độ dài xâu | |
*/ | |
int GetLeng(char *str) | |
{ | |
int leng = 0; | |
while(str[leng] != '\0') leng++; | |
return leng; | |
} | |
/* | |
* Kiểm tra với với vị trí pos trên SB như vậy có thoả mãn hay không | |
* @PARAM: pos : vị trí đang xét trên SB. | |
* RETURN: true nếu hợp lệ, ngược lại là false | |
*/ | |
bool IsValid(int pos) | |
{ | |
int leng1 = LengB - pos; | |
if(leng1 > LengE) return false; | |
for(int i = 0; i < leng1; i++) | |
if(SB[pos + i] != SE[i]) return false; | |
return true; | |
} | |
int main() | |
{ | |
//freopen("input.txt","r",stdin); | |
ios::sync_with_stdio(false); | |
// Nhập đầu vào | |
cin >> SB >> SE; | |
LengB = GetLeng(SB); | |
LengE = GetLeng(SE); | |
// Duyệt xâu SB từ đầu. Tại mỗi vị trí ta sẽ kiểm tra | |
// thành phần cuổi của SB với thành phần đầu tương ứng của SE | |
// - cùng độ dài. Nếu chúng giống nhau thì dừng lại. | |
int pos = 0; | |
for(pos = 0; pos < LengB; pos++) | |
if(IsValid(pos)) break; | |
int MinLen = pos + LengE; | |
// In đầu ra | |
cout << MinLen << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment