Skip to content

Instantly share code, notes, and snippets.

@tekkoc
Created November 10, 2013 14:50
Show Gist options
  • Save tekkoc/7399120 to your computer and use it in GitHub Desktop.
Save tekkoc/7399120 to your computer and use it in GitHub Desktop.
最強最速アルゴリズマー養成講座P63より
import std.stdio;
import std.algorithm;
import std.range;
void main()
{
auto first = ["fishing", "gardening", "swimming", "fishing"];
auto second = ["hunting", "fishing", "fishing", "biting"];
writeln((new InterestingParty()).bestInvitation(first, second));
}
class InterestingParty
{
int bestInvitation(string[] first, string[] second)
{
int[string] counting;
foreach (interesting; zip(first, second)) {
counting[interesting[0]]++;
counting[interesting[1]]++;
}
return counting.values.sort.reverse[0];
}
unittest
{
auto first = ["fishing", "gardening", "swimming", "fishing"];
auto second = ["hunting", "fishing", "fishing", "biting"];
auto result = 4;
assert(result == (new InterestingParty()).bestInvitation(first, second));
}
unittest
{
auto first = ["variety", "diversity", "loquacity", "courtesy"];
auto second = ["talking", "speaking", "discussion", "meeting"];
auto result = 1;
assert(result == (new InterestingParty()).bestInvitation(first, second));
}
unittest
{
auto first = ["snakes", "programming", "cobra", "monty"];
auto second = ["python", "python", "anaconda", "python"];
auto result = 3;
assert(result == (new InterestingParty()).bestInvitation(first, second));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment