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 or size.
An integer M (0 ≤ M ≤ 1,000), representing some quantity or size.
A list of N strings, where each string consists of lower-case letters and has a length between 1 and 10.
A list of M tuples of integers, where each tuple consists of two integers (X, Y), representing some indices (1 ≤ X, Y ≤ 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.
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 of integers
'''
Test Case Generator
Sure! Below is a Python function that generates test cases for the "Palindrome Generator" problem statement. The function `tcgen_palindrome` will create random inputs conforming to the constraints specified in the problem.
```python
import random
import string
def generate_random_word():
length = random.randint(1, 10)
return ''.join(random.choice(string.ascii_lowercase) for _ in range(length))
def tcgen_palindrome():
# 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 = []
for _ in range(M):
x = random.randint(1, N)
y = random.randint(1, N)
pairs.append((x, y))
# Formatting the output similar to the input format of the problem statement
input_data = f"{N} {M}\n"
input_data += '\n'.join(words) + '\n'
input_data += '\n'.join(f"{x} {y}" for x, y in pairs)
return input_data
# Example usage:
print(tcgen_palindrome())
```
This function generates:
1. `N`, the number of words in the dictionary.
2. `M`, the number of ordered pairs of consecutive words.
3. A list of `N` words, each of random length between 1 and 10 characters.
4. A list of `M` ordered pairs, where each pair is a tuple of two integers between 1 and `N`.
The generated input is formatted as a string that mimics the structure of the problem's input format, making it easy to use in testing. You can run `tcgen_palindrome()` to get different random test cases each time.
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
# Example usage:
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# N, M, words, pairs = extract_arguments(fh)
# print(N, M, words, pairs)