Share 49. group anagrams c++,

quocvu937

New member
#C ++, #anagrams, #String, #Programming ## 49. ANOGRAM NHÓM TRONG C ++

Anagrams là những từ được hình thành bằng cách sắp xếp lại các chữ cái của một từ khác.Ví dụ: "Cat" và "TAC" là những kẻ gây rối của nhau.Trong hướng dẫn này, chúng tôi sẽ tìm hiểu cách nhóm các đối thủ trong C ++.

Chúng tôi sẽ sử dụng cấu trúc dữ liệu sau đây để lưu trữ các phương pháp:

`` `
cấu trúc anagram {
Chuỗi từ;
Vector <String> ANAGrams;
};
`` `

Trường `word` lưu trữ từ gốc và trường` anagrams` lưu trữ một vectơ của tất cả các cách viết của từ.

Chúng ta có thể sử dụng thuật toán sau để nhóm ANAGRAMS:

1. Tạo một vectơ của các đối tượng `anagram`.
2. Đối với mỗi từ trong đầu vào, hãy tạo một đối tượng `anagram` mới và thêm nó vào vectơ.
3. Sắp xếp vectơ của các đối tượng `anagram` theo trường` word`.
4Nếu chúng là, hãy thêm đối tượng `anagram` khác vào trường` anagram` của đối tượng `anagram` hiện tại.

Mã sau thực hiện thuật toán này:

`` `C ++
#include <Istream>
#include <Vector>
#include <String>

sử dụng không gian tên STD;

cấu trúc anagram {
Chuỗi từ;
Vector <String> ANAGrams;
};

int main () {
// Tạo một vectơ của các đối tượng đảo chữ.
Vector <Anagram> ANAGrams;

// Thêm các từ đầu vào vào vectơ.
anagrams.push_back ({"cat", {}});
anagrams.push_back ({"tac", {}});
anagrams.push_back ({"hành động", {}});
anagrams.push_back ({"chuột", {}});
anagrams.push_back ({"tar", {}});

// Sắp xếp vectơ của các đối tượng ANAGRAM theo trường từ.
Sắp xếp (anagrams.begin (), anagrams.end (), [] (const anagram & a, const anagram & b) {
trả về a.word <B.word;
});

// Đối với mỗi đối tượng ANAGRAM trong vectơ, lặp qua các đối tượng ANOMRAM khác và kiểm tra xem chúng có phải là đảo chữ của nhau không.Nếu chúng là, hãy thêm đối tượng ANOGRAM khác vào trường ANOMAMMS của đối tượng ANOMRAM HIỆN TẠI.
for (int i = 0; i <anagrams.size (); i ++) {
for (int j = i+1; j <anagrams.size (); j ++) {
if (isanagram (anagrams .word, anagrams [j] .word)) {
anagrams .anagrams.push_back (anagrams [j] .word);
}
}
}

// In kết quả.
for (int i = 0; i <anagrams.size (); i ++) {
cout << anagrams .word << ":";
for (chuỗi anagram: anagrams .anagrams) {
cout << anagram << "";
}
cout << endl;
}

trả lại 0;
}

bool isanagram (chuỗi word1, chuỗi word2) {
if (word1.length ()! = word2.length ()) {
trả lại sai;
}

int đếm [26] = {0};
for (char c: word1) {
Đếm [C - 'A'] ++;
}

for (char c: word2) {
Đếm [c-'a']-;
}

for (int i = 0; i <26
=======================================
#C++, #anagrams, #String, #Programming ## 49. Group Anagrams in C++

Anagrams are words that are formed by rearranging the letters of another word. For example, "cat" and "tac" are anagrams of each other. In this tutorial, we will learn how to group anagrams in C++.

We will use the following data structure to store the anagrams:

```
struct Anagram {
string word;
vector<string> anagrams;
};
```

The `word` field stores the original word, and the `anagrams` field stores a vector of all the anagrams of the word.

We can use the following algorithm to group anagrams:

1. Create a vector of `Anagram` objects.
2. For each word in the input, create a new `Anagram` object and add it to the vector.
3. Sort the vector of `Anagram` objects by the `word` field.
4. For each `Anagram` object in the vector, iterate through the other `Anagram` objects and check if they are anagrams of each other. If they are, add the other `Anagram` object to the `anagrams` field of the current `Anagram` object.

The following code implements this algorithm:

```c++
#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Anagram {
string word;
vector<string> anagrams;
};

int main() {
// Create a vector of Anagram objects.
vector<Anagram> anagrams;

// Add the input words to the vector.
anagrams.push_back({"cat", {}});
anagrams.push_back({"tac", {}});
anagrams.push_back({"act", {}});
anagrams.push_back({"rat", {}});
anagrams.push_back({"tar", {}});

// Sort the vector of Anagram objects by the word field.
sort(anagrams.begin(), anagrams.end(), [](const Anagram& a, const Anagram& b) {
return a.word < b.word;
});

// For each Anagram object in the vector, iterate through the other Anagram objects and check if they are anagrams of each other. If they are, add the other Anagram object to the anagrams field of the current Anagram object.
for (int i = 0; i < anagrams.size(); i++) {
for (int j = i + 1; j < anagrams.size(); j++) {
if (isAnagram(anagrams.word, anagrams[j].word)) {
anagrams.anagrams.push_back(anagrams[j].word);
}
}
}

// Print the results.
for (int i = 0; i < anagrams.size(); i++) {
cout << anagrams.word << ": ";
for (string anagram : anagrams.anagrams) {
cout << anagram << " ";
}
cout << endl;
}

return 0;
}

bool isAnagram(string word1, string word2) {
if (word1.length() != word2.length()) {
return false;
}

int count[26] = {0};
for (char c : word1) {
count[c - 'a']++;
}

for (char c : word2) {
count[c - 'a']--;
}

for (int i = 0; i < 26
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top