HackerRank - Sherlock and Anagram

Please note input constraints. String length will not exceed 100, which means, we can use relatively naive representation\calculation for anagrams: sorting.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

int calc(string &str)
{
    int ret = 0;
    size_t len = str.length();

    unordered_map<string, int> rec;
    for (size_t ilen = 1; ilen < len; ilen++)
    {
        for (size_t i = 0; i <= len - ilen; i++)
        {
            string curr = str.substr(i, ilen);
            std::sort(curr.begin(), curr.end());
            rec[curr]++;
        }

        //
        for (auto &r : rec)
            if (r.second > 1)
                ret += r.second * (r.second - 1) / 2;
        rec.clear();
    }
    return ret;
}

int main()
{
    int n; cin >> n;
    while (n--)
    {
        char buf[102] = { 0 };
        scanf("%s", buf);

        string str(buf);
        int ret = calc(str);
        cout << ret << endl;
    }
    return 0;
}
时间: 2024-10-12 21:39:18

HackerRank - Sherlock and Anagram的相关文章

HackerRank - Sherlock and Queries

All about pruning and duplication removal. Took me several submissions to get it AC: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <unordered_map> using namespace

HackerRank - &quot;Sherlock and GCD&quot;

This is a very smart observation:http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/ # http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/ # from functools import reduce def gcd(a, b): if a == 1 or b == 1: return 1 if a % b

HackerRank - Sherlock and The Beast

Greedy beats DP this time... I tried several DP solutions first, but all failed with RE\TLE. If you 'feel' the problem, Greedy should be working: (A solution from discussion) def getPivot(n): while n > 0: if n % 3 == 0: break; else: n -= 5 return n T

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For

【HackerRank】Sherlock and Array

Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum

【HackerRank】 Sherlock and The Beast

Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has b

【HackerRank】 Game Of Thrones - I

King Robert has 7 kingdoms under his rule. He gets to know from a raven that the Dothraki are going to wage a war against him soon. But, he knows the Dothraki need to cross the narrow river to enter his dynasty. There is only one bridge that connects

Lettocde_242_Valid Anagram

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48979767 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat"

poj 2408 Anagram Groups(hash)

题目链接:poj 2408 Anagram Groups 题目大意:给定若干个字符串,将其分组,按照组成元素相同为一组,输出数量最多的前5组,每组按照字典序输出所 有字符串.数量相同的输出字典序较小的一组. 解题思路:将所有的字符串统计字符后hash,排序之后确定每组的个数并且确定一组中字典序最小的字符串.根据个数 以及字符串对组进行排序. #include <cstdio> #include <cstring> #include <vector> #include &