D. Color with Occurrences(指针,子串)

原题链接:

You are given some text tt and a set of nn strings s1,s2,…,sns1,s2,…,sn.

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

You want to color all the letters of the text tt 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=abat[2…4]=s2=aba in red, we get t=bababat=bababa;
  • Let's color t[1…2]=s1=bat[1…2]=s1=ba in red, we get t=bababat=bababa;
  • Let's color t[4…6]=s2=abat[4…6]=s2=aba in red, we get t=bababat=bababa.

Each string sisi 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 tt in red and how to do it. If it is impossible to color all letters of the text tt in red, output -1.

Input

The first line of the input contains an integer qq (1≤q≤1001≤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 tt (1≤|t|≤1001≤|t|≤100), consisting only of lowercase Latin letters, where |t||t| is the length of the text tt.

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

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

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 mm — the minimum number of steps it will take to turn all the letters tt red.

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

If there are several answers, output any of them.

Example

input

Copy

6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb

output

Copy

3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13

Note

The first test case is explained in the problem statement.

In the second test case, it is impossible to color all the letters of the text in red.

思路:

一定要审清题意,看看输入输出的是什么,这题字符串的常规操作

s.substr(pos,size),返回pos开始,size长度的子串,很方便的

rgt记载可能覆盖的右端点,lst是已经覆盖的右端点,lst《i才进行,这时才可以不重不漏

这题需要整体输出,所以需要用数组存

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxj=2e5+100; 
string a[20];
void solve(){//双指针,子串查找
    string s;cin>>s;
	int n;cin>>n;
	vector<int>ans1,ans2;//审清题意,要输出的是什么
	int lst=-1,rgt=-1;//左右指针
	int id=0,pos=0;
	for(int i=1;i<=n;++i)cin>>a[i];	
	for(int i=0;i<s.size();++i){
		for(int j=1;j<=n;++j){
			if(s.size()-i>=a[j].size())
				if(s.substr(i,a[j].size())==a[j]){
					// cout<<a[j]<<' ';
					int xx=a[j].size()+i-1;
					if(rgt<xx)//能到达最远就最远
					{
						
						rgt=i+a[j].size()-1;
						// cout<<rgt<<' ';
						// cout<<a[j].size()<<' ';
						id=j,pos=i;
					}
				}
				
		}
		if(lst<i){//不重复覆盖
			if(rgt<i){
				cout<<-1<<'\n';
				return ;
			}
			lst=rgt;
			ans1.emplace_back(id);ans2.emplace_back(pos);
		}
	}
	cout<<ans1.size()<<'\n';
	for(int i=0;i<ans1.size();++i){
		cout<<ans1[i]<<' '<<ans2[i]+1<<'\n';
	}
}
signed main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);//a为add,,w
#endif
    int t;
    cin>>t;
    // t=1;
    while(t--){
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值