কম্পিউটার টিউটোরিয়াল

C++-এ ক্যামেলকেস নোটেশনে প্যাটার্ন মিলানো শব্দগুলো প্রিন্ট করুন

ক্যামেলকেস নোটেশনে লেখা স্ট্রিংয়ের একটি অ্যারে এবং একটি প্যাটার্ন দেওয়া আছে। প্যাটার্নের সাথে মিলে যাওয়া স্ট্রিংগুলো বের করতে হবে।

ক্যামেলকেস: প্রতিটি নতুন শব্দের প্রথম অক্ষর বড় হাতের (uppercase), বাকি ছোট হাতের। উদাহরণ: iLoveProgramming।

উদাহরণ: স্ট্রিং: "TutorialsPoint", "ProgrammersPoint", "ProgrammingLover", "Tutorials"। প্যাটার্ন: 'P'। আউটপুট: "TutorialsPoint", "ProgrammersPoint", "ProgrammingLover"।

ট্রি-ভিত্তিক সমাধান

শব্দগুলো ট্রি-তে সংরক্ষণ করে, তারপর বড় হাতের অক্ষর ব্যবহার করে প্যাটার্ন মিলিয়ে শব্দগুলো বের করা হয়।

উদাহরণ কোড
#include <bits/stdc++.h>
using namespace std;

struct TreeNode {
    TreeNode* children[26];
    bool isLeaf;
    list<string> word;
};

TreeNode* getNewTreeNode() {
    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
  1. C++-এ স্ট্রিংকে ব্র্যাকেট দিয়ে সব উপস্ট্রিং আকারে ভাঙা

  2. C++-এ ক্যামেলকেস নোটেশনে প্যাটার্ন মিলানো শব্দগুলো প্রিন্ট করুন

  3. স্ট্রিং-এর সকল সাবস্ট্রিং প্রিন্ট (C++)

  4. সি++ প্রোগ্রাম: ডিকশনারি থেকে নির্দিষ্ট প্যাটার্ন মিলে যাওয়া সব স্ট্রিং খুঁজে বের করা