编程介的小学生 2017-04-11 10:59 采纳率: 20.5%
浏览 812
已采纳

Intricate to death?

Task

Have you heard of the game PAL? Many people believe it's the greatest Chinese RPG series so far. There are many characters, including heros and NPCs, summarized in the following paragraph.

Obviously, some of the descriptions are deliberately lengthened, in order to make the whole paragraph more fun. In this problem, your task is similar: given a set of rules, find a description that is at least A characters and at most B characters long. If there are more than one possible description satisfying the condition above, choose the one that comes first lexicographically. Note that shorter answers are NOT always better than longer ones.

BTW: Rules like "Lixiaoyao: Lixiaoyao" looks rather stupid, so don't print things like that, even if no other solutions can be found.

Input

The first line contains a single integer T (T <= 10), the number of test cases. Each case begins with an integer n (1 <= n <= 5), the number of rules, followed by n lines, each describing a rule. Each rule begins with the character's name, then a colon, then a single space, and the description. The description is a list of tokens separated by a single space, with no leading spaces or trailing spaces, followed by a period. Each token is either a word consisting of only lowercase letters, or a character's name, or a character's name, followed by an apostrophe('), then a lowercase letter 's'. A character's name is always an uppercase letter followed by zero or more lowercase letters. No two characters will have the same name, but not all character names appear in the left side of a rule. After the ruleset, you'll be given a single integer q (1 <= q <= 5) on the next line, indicating the number of queries. Each query appears on its own line, containing the name of the character in question, then two integers A and B (1 <= A <= B <= 100), explained above.

Each rule is at least 15 characters, at most 100 characters, containing at least one lowercase token. On the right side of each rule, there will be at most 3 characters involved, all distinct (though one of them may appear on the left side of the rule), and there are no more than 10000 different descriptions for any character in questions, if we only count descriptions only length <= B.

Output

For each test case, print the case number in the first line, then q answers in the following q lines, one for each line. If there is no solution, print 'No solution.' (without quote), otherwise a sentence describing the character. The sentence should be in the same format as a rule in the input. The length of the sentence (including everything: the character's name, the colon, the space, the description, and the final period) should be at least A and at most B.
Sample Input

2
2
Alice: Bob's mother.
Bob: Alice's son.
2
Alice 20 20
Bob 24 25
4
A: B's girlfriend.
B: A's boyfriend.
C: the only person who knows that A and B love each other.
A: D's smallest daughter.
5
A 1 100
C 1 50
C 70 80
C 71 80
C 82 90
Sample Output

Case 1:
Alice: Bob's mother.
Bob: Bob's mother's son.
Case 2:
A: A's boyfriend's girlfriend.
No solution.
C: the only person who knows that A and A's boyfriend love each other.
C: the only person who knows that B's girlfriend and B love each other.
C: the only person who knows that A and B's girlfriend's boyfriend love each other.

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-04-29 15:48
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法