EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. ABaKoDa
  2. 2023
  3. Round 2

Duel Languages

https://codeforces.com/group/cRJbcAFEwS/contest/487230/problem/D

Question

Somehow, Abby managed to convince Cody that while it is admirable to be proud of his country's culture, his crusade against hiram na titik was starting to border on irrational. She tweeted out that she had finally calmed Cody down and placed three hashtags to reflect her mood. But then Abby ran into her own problems—in his short tyrannical frenzy, Cody had already made many followers and many enemies.

  • When her hashtags contained Abakoda letters (ABKD and their lowercase), people complained, "Sorry sis, parang like I find it medyo hard to speak Tagalog!"

  • When her hashtags contained hiram na titik (CFJQVXZ and their lowercase), people complained, "Eugh, nosebleed! I cannot understand the dire-diretso i-spokening of English!"

Personally, Abby found herself very annoyed with both parties, so she found a way to hopefully shut them both up.

Abby has a pool of nnnn possible words that she's okay with using as hashtags. She must select a first hashtag, a second hashtag, and then a third hashtag, such that the following are all satisfied.

  • The first hashtag contains an Abakoda letter.

  • The second hashtag contains a hiram na titik.

  • The third hashtag contains an Abakoda letter and a hiram na titik.

  • She cannot use the same word in more than one hashtag. In other words, the three chosen hashtags are distinct.

Note that it is okay for the first hashtag to contain a hiram na titik, so long as it has an Abakoda letter, and vice versa for the second hashtag containing an Abakoda letter.

Given Abby's pool of words, help her count all the possible ways to choose the three hashtags. Note that the order of the three hashtags matters. See the explanation of the third sample input for an example.

Input Format

The first line of input contains a single positive integer nnnn.

Then, n lines of input follow, each containing a word that Abby could put in a hashtag.

Constraints

3≤n≤1053 \le n \le 10^53≤n≤105
  • Each word consists of at least 111 and at most 555 upper or lowercase English letters.

  • No two words in the input are the same. Note that this is case-sensitive (so He and he could both appear in the input as different words).

Output Format

Output a single line containing the number of possible ways that Abby can choose her three hashtags.

Sample Inputs:

Input

4
Abkd
Degh
Fil
Rizal

Output

2

Explanation

We can choose either Abkd or Degh as our first hashtag, thus we have two possible choices.

Input

3
Q
q
Qq

Output

0

Explanation

None of the words have an Abakoda letter.

Input

3
AC
Ac
ac

Output

6

Explanation

Any of the words could be the first, second, or third hashtag. We could take them in the order AC, Ac, ac, and because order matters, this would be different from taking them in the order Ac, ac, AC. All possible orderings of them lead to valid ways to choose three hashtags.


Solution — Sets intersection

Here are all the patterns that match with the original problem's requirements, as well as how much each case contributes to the total count.

  1. a_only h_only both

    • a * h * b

  2. a_only both both

    • a * b * (b - 1)

  3. both h_only both

    • b * h * (b - 1)

  4. both both both

    • b * (b - 1) × (b - 2)

Here's the solution for now:

def has_a_letter(word, letters):
    return any(c.upper() in letters for c in word)

abakoda = 'ABKD'
hiram = 'CFJQVXZ'

n = int(input())
a, h, b = 0, 0, 0

for _ in range(n):
    word = input()
    has_abakoda = has_a_letter(word, abakoda)
    has_hiram = has_a_letter(word, hiram)
    if has_abakoda and not has_hiram:
        a += 1
    elif not has_abakoda and has_hiram:
        h += 1
    elif has_abakoda and has_hiram:
        b += 1

total = 0
'''
Here are all the patterns that match with the original problem's requirements, as well as how much each case
contributes to the total count.

• #a_only #h_only #both
    â—¦ a * h * b
• #a_only #both #both
    â—¦ a * b * (b - 1)
• #both #h_only #both
    â—¦ b * h * (b - 1)
• #both #both #both
    ◦ b * (b - 1) × (b - 2)
'''
total += a * h * b
total += a * b * (b-1)
total += b * h * (b-1)
total += b * (b-1) * (b-2)
print(total)

PreviousKensorshipNext2011

Last updated 2 months ago

Page cover image