Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created September 15, 2018 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save completejavascript/64addc7a563a0c926788de6261cfca1e to your computer and use it in GitHub Desktop.
Save completejavascript/64addc7a563a0c926788de6261cfca1e to your computer and use it in GitHub Desktop.
#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