이 문제에서는 낙타 케이스로 된 문자열 배열과 패턴이 제공됩니다. 주어진 패턴과 일치하는 배열의 모든 문자열을 인쇄해야 합니다.
문자열 배열 요소가 문자열 데이터 유형인 배열입니다.
낙타 케이스 프로그래밍에서 이름을 지정하는 일반적인 방법입니다. 이런 식으로 새 단어의 첫 글자는 대문자로 시작하고 나머지는 모두 소문자입니다.
예 − iLove프로그래밍
문제 - 주어진 패턴과 일치하는 모든 문자열을 찾습니다.
예 -
Input : “TutorialsPoint” , “ProgrammersPoint” , “ProgrammingLover” , “Tutorials”. Pattern : ‘P’ Output : “TutorialsPoint” , “ProgrammersPoint” , “ProgrammingLover”
설명 − 'P'가 포함된 모든 문자열을 고려했습니다.
이 문제를 해결하기 위해 사전의 모든 키를 트리에 추가한 다음 대문자를 사용합니다. 그리고 트리의 단어를 처리하고 패턴과 일치하는 모든 것을 인쇄하십시오.
예시
#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
TreeNode* children[26];
bool isLeaf;
list<string> word;
};
TreeNode* getNewTreeNode(void){
TreeNode* pNode = new TreeNode;
if (pNode){
pNode->isLeaf = false;
for (int i = 0; i < 26; i++)
pNode->children[i] = NULL;
}
return pNode;
}
void insert(TreeNode* root, string word){
int index;
TreeNode* pCrawl = root;
for (int level = 0; level < word.length(); level++){
if (islower(word[level]))
continue;
index = int(word[level]) - 'A';
if (!pCrawl->children[index])
pCrawl->children[index] = getNewTreeNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isLeaf = true;
(pCrawl->word).push_back(word);
}
void printAllWords(TreeNode* root){
if (root->isLeaf){
for(string str : root->word)
cout << str << endl;
}
for (int i = 0; i < 26; i++){
TreeNode* child = root->children[i];
if (child)
printAllWords(child);
}
}
bool search(TreeNode* root, string pattern){
int index;
TreeNode* pCrawl = root;
for (int level = 0; level <pattern.length(); level++) {
index = int(pattern[level]) - 'A';
if (!pCrawl->children[index])
return false;
pCrawl = pCrawl->children[index];
}
printAllWords(pCrawl);
return true;
}
void findAllMatch(vector<string> dictionary, string pattern){
TreeNode* root = getNewTreeNode();
for (string word : dictionary)
insert(root, word);
if (!search(root, pattern))
cout << "No match found";
}
int main(){
vector<string> dictionary = { "Tutorial" , "TP" , "TutorialsPoint" , "LearnersPoint", "TutorialsPointsPrograming" , "programmingTutorial"};
string pattern = "TP";
findAllMatch(dictionary, pattern);
return 0;
} 출력
TP TutorialsPoint TutorialsPointsPrograming