D. Color with Occurrences(线性dp)

You are given some text t and a set of n strings s1,s2,…,sn.

In one step, you can choose any occurrence of any string si in the text t and color the corresponding characters of the text in red. For example, if t=bababa and s1=ba, s2=aba, you can get t=bababa, t=bababa or t=bababa in one step.

You want to color all the letters of the text t in red. When you color a letter in red again, it stays red.

In the example above, three steps are enough:

  • Let's color t[2…4]=s2=aba in red, we get t=bababa;

  • Let's color t[1…2]=s1=ba in red, we get t=bababa;

  • Let's color t[4…6]=s2=aba in red, we get t=bababa.

Each string si can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.

Determine the minimum number of steps needed to color all letters t in red and how to do it. If it is impossible to color all letters of the text t in red, output -1.

Input

The first line of the input contains an integer q (1≤q≤100) —the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each test case contains the text t (1≤|t|≤100), consisting only of lowercase Latin letters, where |t| is the length of the text t.

The second line of each test case contains a single integer n (1≤n≤10) — the number of strings in the set.

This is followed by n lines, each containing a string si (1≤|si|≤10) consisting only of lowercase Latin letters, where |si| — the length of string si.

Output

For each test case, print the answer on a separate line.

If it is impossible to color all the letters of the text in red, print a single line containing the number -1.

Otherwise, on the first line, print the number m — the minimum number of steps it will take to turn all the letters t red.

Then in the next m lines print pairs of indices: wj and pj (1≤jm), which denote that the string with index wj was used as a substring to cover the occurrences starting in the text t from position pj. The pairs can be output in any order.

If there are several answers, output any of them.

一个字符串t和n个子字符串,用子字符串给t上色,操作次数要求最小使字符串t变为红色

输出操作次数 操作位置和选择的子字符串的序列号

求出字符串 t 每个字符最长前缀下标 i, i 到当前字符能被子字符串上色。然后从结尾倒着一直往前跳就能求出答案。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
int dp[110];\\变到该位置的最优情况 
PII pr[110];
void solve()
{
    string t;
    int n;
    cin>>t>>n;
    vector<string>s(n);
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    int len=t.size();
    for(int i=1;i<=len;i++)
    {
        dp[i]=0x3f3f3f3f;
    }
    for(int i=0;i<len;i++)\\枚举开始改变的位置 
    {
        for(int j=0;j<n;j++)\\枚举n个子字符串 
        {
            if(i+s[j].size()<=len&&t.substr(i,s[j].size())==s[j])\\判断是否符合规则 
            {
                for(int k=1;k<=s[j].size();k++)\\满足情况的位置都可以做出改变 
                {
                    if(dp[i+k]>dp[i]+1)\\求解最优情况 
                    {
                        dp[i+k]=dp[i]+1;
                        pr[i+k]={j,i};
                    }    
                }
            }
        }
    }

    if(dp[len]==0x3f3f3f3f)
    {
        cout<<-1<<endl;
        return ; 
    }
    vector<PII>v;
    while(len>0)
    {
        v.push_back(pr[len]);
        len=pr[len].second;
    }
    len=t.size();
    cout<<dp[len]<<endl;
    for(auto i:v)
    {
        cout<<i.first+1<<" "<<i.second+1<<endl;
    }    
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值