Problem p01424 - Generation 3

Orig Description

Palindrome Generator
A palidrome craftsperson starts to work in the early morning, 
with the clear air allowing him to polish up his palindromes.
On this morning, he is making his pieces to submit to the International Contest for Palindrome Craftspeople.
By the way, in order to make palindromes, he uses a special dictionary which contains a set of words and a set of ordered pairs of the words.
Any words and any ordered pairs of consecutive words in his palindromes must appear in the dictionary.
We already have his dictionary, so let's estimate how long a palindrome he can make.
Input
The first line in the data set consists of two integers N (1 \leq N \leq 100) and M (0 \leq M \leq 1\,000).
N describes the number of words in the dictionary and M describes the number of ordered pairs of words.
The following N lines describe the words he can use.
The i-th line (1-based) contains the word i, which consists of only lower-case letters and whose length is between 1 and 10, inclusive.
The following M lines describe the ordered pairs of consecutive words he can use.
The j-th line (1-based) contains two integers X_j and Y_j (1 \leq X_j, Y_j \leq N).
X_j describes the (1-based) index of the former word in the palindrome and Y_j describes that of the latter word.
Output
Print the maximal length of the possible palindrome in a line.
If he can make no palidromes, print "0".
If he can make arbitrary long palindromes, print "-1". 
Sample Input 1
2 2
ab
ba
1 1
1 2
Output for the Sample Input 1
4
Sample Input 2
2 2
ab
ba
1 1
2 2
Output for the Sample Input 2
0
Sample Input 3
2 2
ab
a
1 1
1 2
Output for the Sample Input 3
-1

Extracted Specification

An integer N (1 ≤ N ≤ 100), representing some quantity.
An integer M (0 ≤ M ≤ 1000), representing another quantity.
A list of N strings, where each string is between 1 and 10 characters long and consists of only lower-case letters.
A list of M tuples, where each tuple contains two integers (X_j, Y_j) (1 ≤ X_j, Y_j ≤ N).

### Example Input:

```
2
2
ab
ba
1 1
1 2
```

### Function Signature:
Write a function f(N, M, words, pairs) that takes in the input.
```python
def f(N: int, M: int, words: List[str], pairs: List[Tuple[int, int]]):
    '''
    N: an integer
    M: an integer
    words: a list of strings
    pairs: a list of tuples containing two integers each
    '''
```

Test Case Generator

Certainly! Here is a Python function that generates random test cases for the "Palindrome Generator" problem statement:

```python
import random
import string

def generate_random_word():
    word_length = random.randint(1, 10)
    return ''.join(random.choices(string.ascii_lowercase, k=word_length))

def tcgen_palindrome_generator():
    # Generate N and M
    N = random.randint(1, 100)
    M = random.randint(0, 1000)
    
    # Generate N words
    words = [generate_random_word() for _ in range(N)]
    
    # Generate M ordered pairs
    pairs = [(random.randint(1, N), random.randint(1, N)) for _ in range(M)]
    
    return N, M, words, pairs

# Convert the generated test case to the desired input format
def format_test_case(N, M, words, pairs):
    test_case = f"{N} {M}\n"
    test_case += "\n".join(words) + "\n"
    test_case += "\n".join(f"{x} {y}" for x, y in pairs)
    return test_case

# Example usage:
N, M, words, pairs = tcgen_palindrome_generator()
formatted_test_case = format_test_case(N, M, words, pairs)
print(formatted_test_case)
```

This script generates a random test case for the "Palindrome Generator" problem. The test case includes:

1. An integer \( N \) representing the number of words in the dictionary.
2. An integer \( M \) representing the number of ordered pairs of words.
3. A list of \( N \) words, each consisting of 1 to 10 lowercase letters.
4. A list of \( M \) ordered pairs, each pair consists of two integers representing the indices of the words in the dictionary.

You can run this script to generate different test cases for the problem. The `format_test_case` function formats the generated test case into the required input format.

Extract Arguments

def extract_arguments(fh):
    N, M = map(int, fh.readline().strip().split())
    words = [fh.readline().strip() for _ in range(N)]
    pairs = [tuple(map(int, fh.readline().strip().split())) for _ in range(M)]
    return N, M, words, pairs

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh: 
#     N, M, words, pairs = extract_arguments(fh)
#     f(N, M, words, pairs)