编程介的小学生 2018-12-18 13:05 采纳率: 20.5%
浏览 382
已采纳

根据输入的字符串判断它是否和下面的能够匹配,这个问题解决的思路是什么?怎么写出来?

Problem Description

You are given a long string and looking for certain patterns in the string.
The string contains only lowercase letters (a−z), and it is represented in a compressed format. Denoting S1,S2,... as compressed strings, another compressed string S is defined recursively in one of the following ways:

⋅ S can be any string consisting of only lowercase letters (a−z).
⋅ S can be generated by repeating another string for any times. Specifically, S is represented as “R(S1)”, which means that the content of S1 is repeated R times.
⋅ S can also be the concatenation of other strings. Specifically, S is represented as “S1,S2...SL”, which means S is the concatenation of S1,S2,...,SL.
⋅An empty string (“”) is also a valid representation.

Formally, the Backus–Naur Form (BNF) specification of the syntax is

::= “” | | | “(” “)”

For example, the string “baaabbaaab” can be compressed as “b3(a)2(b)3(a)b”. It can also be compressed as “2(b3(a)b)”.

On the other hand, you find deterministic finite automaton (DFA) as powerful way to describe the patterns you are looking for. A DFA contains a finite set of states Q and a finite set of input symbols called the alphabet Σ. Initially, the DFA is positioned at the start state q0∈Q. Given the transition function δ(q,a) and an input symbol a, the DFA transit to state δ(q,a) if its current state is q.

Let w=a1a2...an be a string over the alphabet Σ. According to the above definition, the DFA transits through the following sequence of states.

q0,q1=δ(q0,a1),q2=δ(q1,a2),…,qn=δ(q(n−1),an)

The DFA also contains a set of accept states F⊆Q. If the last state qn is an accept state, we say that the DFA accepts the string w. The set of accepted strings is referred as the language that the DFA represents.

Now you are given a compressed string S and a DFA A. You want to know if A accepts the decompressed content of S.

Input

The first line of input contains a number T indicating the number of test cases (T≤200).

The first line of each test case contains a non-empty compressed string S, as described above. The length of S is not greater than 10000, and 0≤R≤109. It is guaranteed that the representation of S is valid.

The description of the DFA follows.

The first line of the description contains three integers N, M, and K, indicating the number of states, the number of rules describing the transition function, and the number of accept states (1≤K≤N≤1000,0≤M≤26N). The states are numbered from 0 to N−1. The start state is always 0.

The second line contains K integers representing the accept states. All these numbers are distinct.

Each of the next M lines consists of two states p and q, and an input symbol a, which means that the DFA transits from p to q when it receives the symbol a. The symbol a is always a lowercase letter. It is guaranteed that, given p and a, the next state q is unique.

Output

For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is “Yes” if the DFA accepts the string, or “No” otherwise.

Sample Input

3
2(b3(a)b)
2 3 1
0
0 1 b
1 0 b
1 1 a
b3(a)2(b)3(a)b
2 2 1
1
0 1 b
1 0 a
b3(a)2(b)3(a)b
2 4 1
0
0 1 b
0 1 a
1 0 a
1 0 b

Sample Output

Case #1: Yes
Case #2: No
Case #3: Yes

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-08-24 23:50
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)