1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Karolina [17]
3 years ago
5

Write a C++ program that searches for anagrams in a dictionary. An anagram is a word obtained by scrambling the letters of some

string.
Computers and Technology
1 answer:
Semmy [17]3 years ago
8 0

Answer:

d, avt, car, feet, more, pitch, rome, tac, teef,

Anagrams of b in dictionary

Anagrams of cat in dictionary

avt, cat, tac,

Anagrams of room in dictionary

more, rome,

Anagrams of hello in dictionary

Anagrams of  in dictionary

Explanation:

// FindAnagrams.cpp : it is the starting point of application console.

//

#include <vector>

#include <hash_map>

#include <iostream>

#include <string>

#include <algorithm>

#include <map>

#include <set>

using namespace std;

using namespace stdext;

bool IsAnagrams(string& strA, string& strB)

{

   list<char> listA(strA.begin(), strA.end());

   list<char> listB(strB.begin(), strB.end());

   listA.sort();

   listB.sort();

   return listA == listB;

   // return equal(listA.begin(), listA.end(), listB.begin());

}

string SortChars(string str)

{

   list<char> l(str.begin(), str.end());

   l.sort();

   return string(l.begin(), l.end());

}

set<string> FindAnagrams(list<string>& dict, string findStr)

{

   map<string, set<string>> d;

   typedef pair<string, set<string>> MyPair;

   for(list<string>::const_iterator it = dict.begin(); it != dict.end(); ++it){

       string str(SortChars(*it));

       if(d.find(str) == d.end()){

           set<string> s;

           s.insert(*it);

           d.insert(MyPair(str, s));

       }

       else{

           d[str].insert(*it);

       }

   }

   string sortedStr(SortChars(findStr));

   return d[sortedStr];

}

int main(int argc, char* argv[])

{

   list<string> dict;

   dict.push_back("c");

   dict.push_back("car");

   dict.push_back("avt");

   dict.push_back("taac");

   dict.push_back("feet");

   dict.push_back("teef");

   dict.push_back("rom");

   dict.push_back("more");

   dict.push_back("pit");

   dict.sort();

   cout << "The dictionary: " << endl;

   copy(dict.begin(), dict.end(), ostream_iterator<string>(cout, ", "));

   cout << endl;

   list<string> testCases;

   testCases.push_back("d");

   testCases.push_back("car");

   testCases.push_back("rome");

   testCases.push_back("hell");

   testCases.push_back("");

   for(list<string>::iterator it = testCases.begin(); it != testCases.end(); ++it)

   {

       cout << endl << "Anagrams of " << *it << " in dictionary" << endl;

       set<string> output = FindAnagrams(dict, *it);

       copy(output.begin(), output.end(), ostream_iterator<string>(cout, ", "));

       cout << endl;

   }

   return 0;

}

You might be interested in
write a C program the prints out the size of variables with the following C data types- int, long, unsigned, long long, double,
Anit [1.1K]

<u>C program for finding size of different data types</u>

#include <stdio.h>

//driver function

int main()

{

   int a;  //declaring a of type int

   float b; //declaring b of type float

   double c; //declaring c of type double

   char d; //declaring d of type char

   long e; //declaring e of type long

   long long f; //declaring f of type longlong

   unsigned g; //declaring g of type unsigned

   // Sizeof operator is used to evaluate the size of a variable

printf(" int data type contains: %lu bytes\n",sizeof(a));/*Finding size of int */

printf("float data type contains : %lu bytes\n",sizeof(b));/*Finding size of float */

printf("double data type contains: %lu bytes\n",sizeof(c));/*Finding size of double */

printf("char data type contains: %lu byte\n",sizeof(d));/*Finding size of char */

printf("long data type contains: %lu byte\n",sizeof(e));/*Finding size of long*/ printf("longlong data type contains: %lu byte\n",sizeof(f));/*Finding size of longlong */

printf("unsigned data type contains: %lu byte\n",sizeof(g)); /*Finding size of unsigned */

   return 0;

}

<u>Output</u>

int data type contains: 4 bytes

float data type contains : 4 bytes

double data type contains: 8 bytes

char data type contains: 1 byte

long data type contains: 8 byte

longlong data type contains: 8 byte

unsigned data type contains: 4 byte

4 0
2 years ago
What term identifies the physical interface between a computer and its peripherals?
antoniya [11.8K]
(B. Hardware), Hardware is physical components, Software are the programs in the computer.
3 0
2 years ago
Which of the following is an external hard drive
fredd [130]

Answer:

Question 1 = D

Explanation:

7 0
2 years ago
Heya!!<br><br>•DEFINE DATA SCIENCE??<br>(∩_∩)<br><br>#kavya#<br>​
anastassius [24]

Answer:

Explanation:

Data science defined

Data science encompasses preparing data for analysis, including cleansing, aggregating, and manipulating the data to perform advanced data analysis. Analytic applications and data scientists can then review the results to uncover patterns and enable business leaders to draw informed insights.

5 0
2 years ago
What kinds of circumstances would lead you to writing a function versus using a loop? please explain in simple terms
GREYUIT [131]

Answer:

1. You want to use parameters

2. You don't want your program to run multiple times

3. You want to call that snippet of code throughout your program

hope this helped :D

5 0
2 years ago
Other questions:
  • The protocol that makes it possible for a Macintosh web browser to be able to retrieve a Web page from a Microsoft Web server is
    8·2 answers
  • Double clicking a word selects the entire word?
    9·2 answers
  • When you are working on an unsaved document on a PC, where is the document temporarily saved?
    5·1 answer
  • Which of the following is used to encrypt web application data?
    11·1 answer
  • _______ data would be useful for creating a report containing last year's revenue, which won't be changing.      A. Integrated B
    12·1 answer
  • Which devices typically generate computer output ?
    8·2 answers
  • There have not been any changes to instruments or music in the last 50 years. The technology in music is still the same.
    12·1 answer
  • 1.Which one of the following buttons returns a window to its original size?
    11·1 answer
  • Why media is far from government​
    6·2 answers
  • Brianna is feeling overwhelmed by the amount of digital
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!