📜  在给定的句子中查找带有大多数字谜的单词

📅  最后修改于: 2021-04-23 18:30:08             🧑  作者: Mango

给定句子形式的字符串S ,任务是从文本中找到具有最大字谜数量的单词 出现在给定的句子中。
例子:

方法:
需要进行以下观察以解决该程序:

请按照以下步骤解决问题:

  • 为每个字母分配一个不同的素数
  • 对于给定字符串中的每个单词,计算分配给该单词的字符的素数乘积,并将其存储在HashMap中
  • 计算分配给单词字符的质数的乘积,并将其存储在HashMap中
  • HashMap中找到频率最高的产品,并打印相应的字谜之一作为答案。

下面是上述方法的实现:

C++14
// C++ Program to find the word
// with most anagrams in a sentence
#include 
using namespace std;
 
// Function to find the word with
// maximum number of anagrams
string largestAnagramGrp(vector arr)
{
   
  // Primes assigned to 26 alphabets
  int prime[] = {2,  3,  5,  7,  11, 13, 17,
                19, 23, 29, 31, 37, 41,
                43, 47, 53, 59, 61, 67, 71,
                73, 79, 83, 89, 97, 101};
  int max = -1;
  long maxpdt = -1;
 
  // Stores the product and
  // word mappings
  unordered_map W;
 
  // Stores the frequencies
  // of products
  unordered_map P;
  for (string temp : arr)
  {
    long pdt = 1;
 
    // Calculate the product of
    // primes assigned
    for (char t : temp)
    {
      pdt *= prime[t - 'a'];
    }
 
    // If product already exists
    if (P.find(pdt) != P.end())
    {
      P[pdt]++;
    }
 
    // Otherwise
    else
    {
      W[pdt] = temp;
      P[pdt] = 1;
    }
  }
 
  // Fetch the most frequent product
  for (auto e : P)
  {
    if (max < e.second)
    {
      max = e.second;
      maxpdt = e.first;
    }
  }
 
  // Return a string
  // with that product
  return W[maxpdt];
}
 
// Driver Code
 
int main()
{
  char S[] = "please be silent and listen to what the professor says ";
  vector arr;
 
  char *token = strtok(S, " ");
  while (token != NULL)
  {
    arr.push_back(token);
    token = strtok(NULL, " ");
  }
 
  cout << largestAnagramGrp(arr) << endl;
  return 0;
}
 
// This code is contributed by
// sanjeev2552


Java
// Java Program to find the word
// with most anagrams in a sentence
import java.util.*;
 
class GFG {
 
    // Function to find the word with
    // maximum number of anagrams
    private static String largestAnagramGrp(
        String arr[])
    {
        // Primes assigned to 26 alphabets
        int prime[]
            = { 2, 3, 5, 7, 11, 13, 17,
                19, 23, 29, 31, 37, 41,
                43, 47, 53, 59, 61, 67, 71,
                73, 79, 83, 89, 97, 101 };
 
        int max = -1;
        long maxpdt = -1;
 
        // Stores the product and
        // word mappings
        HashMap W
            = new HashMap<>();
 
        // Stores the frequencies
        // of products
        HashMap P
            = new HashMap<>();
 
        for (String temp : arr) {
            char c[] = temp.toCharArray();
            long pdt = 1;
 
            // Calculate the product of
            // primes assigned
            for (char t : c) {
                pdt *= prime[t - 'a'];
            }
 
            // If product already exists
            if (P.containsKey(pdt)) {
                P.put(pdt, P.get(pdt) + 1);
            }
 
            // Otherwise
            else {
                W.put(pdt, temp);
                P.put(pdt, 1);
            }
        }
 
        // Fetch the most frequent product
        for (Map.Entry
                 e : P.entrySet()) {
            if (max < e.getValue()) {
                max = e.getValue();
                maxpdt = e.getKey();
            }
        }
 
        // Return a string
        // with that product
        return W.get(maxpdt);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        String S = "please be silent and listen"
                   + " to what the professor says ";
        String arr[] = S.split("[ ]+");
        System.out.println(largestAnagramGrp(arr));
    }
}


Python3
# Python3 Program to find the word
# with most anagrams in a sentence
 
# Function to find the word with
# maximum number of anagrams
def largestAnagramGrp(arr):
     
    # Primes assigned to 26 alphabets
    prime= [2, 3, 5, 7, 11, 13, 17,
            19, 23, 29, 31, 37, 41,
            43, 47, 53, 59, 61, 67, 71,
            73, 79, 83, 89, 97, 101]
    max = -1
    maxpdt = -1
 
    # Stores the product and
    # word mappings
    W = {}
 
    # Stores the frequencies
    # of products
    P = {}
    for temp in arr:
        c = [i for i in temp]
        pdt = 1
 
        # Calculate the product of
        # primes assigned
        for t in c:
            pdt *= prime[ord(t) - ord('a')]
 
        # If product already exists
        if (pdt in P):
            P[pdt] = P.get(pdt, 0) + 1
 
        # Otherwise
        else:
            W[pdt] = temp
            P[pdt] = 1
 
    # Fetch the most frequent product
    for e in P:
        if (max < P[e]):
            max = P[e]
            maxpdt = e
 
    # Return a string
    # with that product
    return W[maxpdt]
 
# Driver Code
if __name__ == '__main__':
    S = "please be silent and listen to what the professor says"
    arr = S.split(" ")
    print(largestAnagramGrp(arr))
 
    # This code is contributed by mohit kumar 29


C#
// C# program to find the word
// with most anagrams in a sentence
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the word with
// maximum number of anagrams
private static String largestAnagramGrp(String []arr)
{
     
    // Primes assigned to 26 alphabets
    int []prime = { 2, 3, 5, 7, 11, 13, 17,
                    19, 23, 29, 31, 37, 41,
                    43, 47, 53, 59, 61, 67, 71,
                    73, 79, 83, 89, 97, 101 };
 
    int max = -1;
    long maxpdt = -1;
 
    // Stores the product and
    // word mappings
    Dictionary W = new Dictionary();
 
    // Stores the frequencies
    // of products
    Dictionary P = new Dictionary();
                                        
    foreach(String temp in arr)
    {
        char []c = temp.ToCharArray();
        long pdt = 1;
 
        // Calculate the product of
        // primes assigned
        foreach(char t in c)
        {
            pdt *= prime[t - 'a'];
        }
 
        // If product already exists
        if (P.ContainsKey(pdt))
        {
            P[pdt] = P[pdt] + 1;
        }
 
        // Otherwise
        else
        {
            W.Add(pdt, temp);
            P.Add(pdt, 1);
        }
    }
 
    // Fetch the most frequent product
    foreach(KeyValuePair e in P)
    {
        if (max < e.Value)
        {
            max = e.Value;
            maxpdt = e.Key;
        }
    }
 
    // Return a string
    // with that product
    return W[maxpdt];
}
 
// Driver Code
public static void Main(String []args)
{
    String S = "please be silent and listen" +
               " to what the professor says ";
                
    String []arr = S.Split(' ');
     
    Console.WriteLine(largestAnagramGrp(arr));
}
}
 
// This code is contributed by sapnasingh4991


输出:
silent

时间复杂度: O(N),N是字符串的长度,不包括空格。
辅助空间: O(N)