编程介的小学生 2017-08-20 09:17 采纳率: 20.5%
浏览 792
已采纳

Another Letter Tree

Problem Description
A letter tree is given on this page (HDOJ 4601).

Another letter tree of N nodes is now given, where each node is assigned to a lowercase letter. When making a tour from a node to another, one should always follow the shortest path between them. That is, no nodes should be visited twice during his tour.

After one travels along a path, he will obtain a Path String formed by the letters assigned to nodes that he just moves past. The string exactly records all nodes in the order he visits.

Now we’re faced with the problem. Initially we have a string S0 and totally Q queries each in a shape of (u, v). For each query, we make a tour from u to v and obtain a Path String Suv , and your task is to calculate how many times that S0 occurs as a subsequence of Suv . For example, “ab” occurs 3 times in “abab” as a subsequence.

A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For instance, “abc” and “ec” are both subsequence of “adebc”, while “ba” and “ed” are not.

The answer might be large, and thus you’re only required to output it modulo 10007.

Input
The input consists of several test cases. The number of test cases T (T<=5) occurs in the first line of input.

For each test case:
The first line consists of two integers N and Q(1<=N, Q<=50000), denoting the number of nodes and queries, respectively.
Each of the following N - 1 lines contains a pair of integers u, v(1<=u, v<=N ), denoting an edge between node u and v.
The following line contains a string of N lowercase letters, where the ith denotes the letter represented by the ith node.
The following line contains S0(1<=|S0|<=30).
The following Q lines describe all queries, each of which describes the start node u and destination node v.

Output
For each query, output the required answer modulo 10007 in a line.

Sample Input
2
6 3
1 2
1 3
2 4
2 5
3 6
abbaaa ab
4 5
4 6
2 1
6 3
1 2
1 3
2 4
2 5
3 6
abbaaa aba
4 5
4 6
2 1

Sample Output
1
3
0
1
4
0

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测