Running Track CodeForces - 615C

A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.

First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.

Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.

Input

First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.

Output

The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.

If the answer is not -1, then the following n lines should contain two integers xi and yi — numbers of ending blocks in the corresponding piece. If xi ≤ yi then this piece is used in the regular order, and if xi > yi piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.

Example

Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1

Note

In the first sample string "cbaabc" = "cba" + "abc".

In the second sample: "ayrat" = "a" + "yr" + "at".

题解:竟然是暴力加贪心,,,coma[i][j]:以b[ i ] (a[ j ] = b[ i ] ) 为起点的相同的序列的长度。comb数组同理,只不过是方向相反但序列相同(ex:abc,cba)。然后遍历b串,依次匹配。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int,int> P;
 4 
 5 const int maxn=2200;
 6 
 7 int na,nb;
 8 int coma[maxn][maxn],comb[maxn][maxn];
 9 string a,b;
10 
11 int main()
12 {   cin>>a>>b;
13     na=a.size();
14     nb=b.size();
15     a='#'+a+'$';
16     b='!'+b+'@';
17     for(int i=nb;i>=1;i--){
18         for(int j=na;j>=1;j--)
19             if(b[i]==a[j]) coma[i][j]=coma[i+1][j+1]+1;
20         for(int j=1;j<=na;j++)
21             if(b[i]==a[j]) comb[i][j]=comb[i+1][j-1]+1;
22     }
23     vector<P> q;
24     for(int i=1;i<=nb;){
25         int x=max_element(coma[i]+1,coma[i]+na+1)-coma[i];
26         int y=max_element(comb[i]+1,comb[i]+na+1)-comb[i];
27         int len=max(coma[i][x],comb[i][y]);
28         if(len==0){
29             cout<<"-1"<<endl;
30             return 0; 
31         }
32         if(len==coma[i][x])
33             q.push_back(P(x,x+len-1));
34         else
35             q.push_back(P(y,y-len+1));
36         i=i+len;
37     }
38     cout<<q.size()<<endl;
39     for(int i=0;i<q.size();i++) cout<<q[i].first<<" "<<q[i].second<<endl;
40     return 0;
41 }

 

 

转载于:https://www.cnblogs.com/zgglj-com/p/7715414.html

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值