编程介的小学生 2019-12-18 00:02 采纳率: 0.4%
浏览 72

Bonus Word 代码的问题

Problem Description
Lingo is a once popular game show where the contestants have to guess words. In the original version the contestants would have to guess a five-letter word each round.
In between the rounds of regular word guessing, the contestants can win a bonus prize if they can guess a ten-letter word. The ten-letter word is displayed with the letters permuted. Some letters are colored indicating that they are displayed in the right position. Since there are not that many ten-letter words, it happens frequently that the word is actually a compound: a word constructed by concatenating two shorter words. In this problem we
assume that the ten-letter word is always of this form.
Given a dictionary and a sequence of ten letters, you must calculate the possible solutions to the ten-letter word game. Two solutions are considered different if they are constructed from different parts, even if their concatenation is the same. This is illustrated by the the second sample case.

Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:

One line with an integer n (1 <= n <= 200): the number of words in the dictionary.

n lines with a single word in the dictionary. Each word is between 1 and 9 (inclusive) characters long and consists of only lowercase letters.

One line with an integer q (1 <= q <= 100): the number of queries.

q lines with a single query string. Each query is exactly 10 characters long and will consist of uppercase and lowercase letters. Lowercase letters are in the right position and uppercase letters may be in the wrong position.
All words in the dictionary for a single test case are distinct.

Output
For each test case, output for each query:

One line with an integer s: the number of solutions.

min(1000, s) lines, each a solution formatted as two dictionary words separated by a hyphen (-).

The solutions to a single query must be ordered lexicographically. If the number of solutions exceeds 1000, then only output the first 1000 solutions.

Notes
If s and t are strings of equal length and si denotes the ith character of s, then s precedes t lexicographically if for some i: si < ti and sj = tj for all j < i. In this problem statement, the hyphen precedes all letters lexicographically.

Sample Input
2
5
gunner
integral
relating
tail
un
4
TAILGUNNER
unINTEGRAL
UNrelating
IMPOSSIBLE
3
aaaa
aaaaa
aaaaaa
1
AAAAAAAAAA

Sample Output
6
gunner-tail
integral-un
relating-un
tail-gunner
un-integral
un-relating
2
un-integral
un-relating
1
un-relating
0
3
aaaa-aaaaaa
aaaaa-aaaaa
aaaaaa-aaaa

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-28 14:06
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # Import necessary packages
    library(readline)
    
    # Define function to check if a word is a valid solution
    is_valid_solution <- function(word) {
        # Split the word into its components
        components <- strsplit(word, "-")[[1]]
        
        # Check if the components are in order
        return(all(c(comp[1] == comp[-1] for comp in components)))
    }
    
    # Define function to generate all possible permutations of a given set of letters
    generate_permutations <- function(set_letters) {
        # Generate all permutations of the set_letters using a recursive approach
        permutations <- c()
        while(TRUE) {
            # Try adding the next letter to each permutation until no more letters remain
            add_letter <- function() {
                new_permutation <- permutations[-c(permutations)]
                new_permutation[[length(new_permutation)]] <- set_letters[length(set_letters)]
                permutations <- c(permutations, new_permutation)
            }
            
            # Add the current letter to each permutation
            add_letter()
            
            # Stop if no more letters remain
            break
        }
        
        # Return the final permutations
        return(permutations)
    }
    
    # Main function to solve the problem
    solve_bonus_word_game <- function(t, d, q) {
        # Initialize variables
        results <- matrix(ncol=100, nrow=q)
        num_words <- sum(q > 0)
        
        # Iterate over each query
        for(i in 1:min(q)) {
            # Extract query string
            query_string <- readline(prompt="Enter query string: ")
            
            # Initialize variables for the current query
            word_solutions <- numeric(num_words)
            max_length <- min(length(query_string), 10)
            
            # Generate permutations for the query string
            permutations <- generate_permutations(strsplit(query_string, "-")[[1]])
            permutations <- permutations[sapply(permutations, function(x) !any(is.na(x))),]
            
            # Iterate over each permutation
            for(j in 1:length(permutations)) {
                # Extract the permutation
                permutation <- permutations[j, ]
                
                # Calculate the score based on the presence or absence of correct positions
                score <- sum(permutation[1:max_length])
                
                # Determine whether the permutation is a valid solution
                is_valid <- is_valid_solution(stringi(substitute(paste(permutation[1:max_length], collapse="-")), 
                                                    replace=TRUE))
                
                # Store the score and validity information in the results vector
                results[i, ] <- c(score, is_valid)
            }
            
            # Sort the results by score in descending order
            results[, order(-results[, 1])] <- NULL
            
            # Print the results for the current query
            print(results[i, ])
        }
        
        # Return the total number of solutions found
        num_words
    }
    
    # Read input data
    t <- readline(prompt="Enter number of test cases: ")
    
    # Solve the problem for each test case
    for(i in 1:t) {
        d <- readline(prompt="Enter dictionary: ")
        q <- readline(prompt="Enter number of queries: ")
        
        # Convert the input data to a list
        d_list <- readLines(d)
        q_list <- readLines(q)
        
        # Solve the problem
        results <- solve_bonus_word_game(t, d_list, q_list)
        
        # Print the results for the current test case
        print(results)
    }
    

    This code defines a function solve_bonus_word_game that takes three arguments: t, d, and q. It reads input data from the user, solves the problem for each test case, and prints the results for each query. The code also includes helper functions is_valid_solution and generate_permutations to validate solutions and generate permutations, respectively.

    评论

报告相同问题?