思路:

kmp + 二分。

实现:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = ;
int neXt[MAXN]; void getNext(string s)
{
int n = s.length();
neXt[] = -;
int k = -, j = ;
while (j < n)
{
if (k == - || s[j] == s[k])
{
j++; k++;
if (s[j] != s[k]) neXt[j] = k;
else neXt[j] = neXt[k];
}
else
{
k = neXt[k];
}
}
} int kmp(string s, string p)
{
int i = , j = ;
int m = s.length(), n = p.length();
while (i < m && j < n)
{
if (j == - || s[i] == p[j]) i++, j++;
else j = neXt[j];
}
if (j == n) return i - j;
return -;
} bool solve(string x, string y, vector<pair<int, int>> & ans)
{
string z(x);
reverse(z.begin(), z.end());
int m = x.length();
int n = y.length();
int start = ;
while (start < n)
{
int l = , r = n - start, res = , pos = -;
bool flg = true;
while (l <= r)
{
int mid = (l + r) >> ;
string tmp = y.substr(start, mid);
getNext(tmp);
int p = kmp(x, tmp);
if (p != -)
{
res = mid; pos = p; flg = true; l = mid + ;
}
else if ((p = kmp(z, tmp)) != -)
{
res = mid; pos = p; flg = false; l = mid + ;
}
else r = mid - ;
}
if (!res) return false;
if (flg) ans.push_back(pair<int, int>(pos + , pos + res));
else ans.push_back(pair<int, int>(m - pos, m - pos - res + ));
start += res;
}
return true;
} int main()
{
string x, y;
cin >> x >> y;
vector<pair<int, int>> res;
if (solve(x, y, res))
{
cout << res.size() << endl;
for (int i = ; i < res.size(); i++)
cout << res[i].first << " " << res[i].second << endl;
}
else
{
puts("-1");
}
return ;
}

CF615C Running Track的更多相关文章

  1. Codeforces Round #338 (Div. 2) C. Running Track dp

    C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...

  2. 【28.57%】【codeforces 615C】 Running Track

    time limit per test1 second memory limit per test512 megabytes inputstandard input outputstandard ou ...

  3. Codeforces 615C Running Track(DP + Trie树)

    题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...

  4. Codeforces Round #338 (Div. 2)

    水 A- Bulbs #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1 ...

  5. 基于智能手机的3D地图导航

    https://www.gpsworld.com/resources/archives/ Going 3D Personal Nav and LBS To enrich user experience ...

  6. 智课雅思词汇---十二、vent是什么意思

    智课雅思词汇---十二.vent是什么意思 一.总结 一句话总结:词根:ven, vent = come, 表示“来” 词根:vent = wind 风 1.tact是什么意思? 词根:-tact-, ...

  7. POJ 题目3661 Running(区间DP)

    Running Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5652   Accepted: 2128 Descripti ...

  8. Running Kafka At Scale

    https://engineering.linkedin.com/kafka/running-kafka-scale If data is the lifeblood of high technolo ...

  9. Running a Remote Desktop on a Windows Azure Linux VM (远程桌面到Windows Azure Linux )-摘自网络(试了,没成功 - -!)

                              A complete click-by-click, step-by-step video of this article is available ...

随机推荐

  1. 【Codeforces 598D】Igor In the Museum

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 同一个联通块里面答案都一样. 把每个联通块的答案都算出来 然后赋值就好 [代码] #include <bits/stdc++.h> ...

  2. Failed to load slave replication state from table mysql.gtid_slave_pos: 1146: Table 'mysql.gtid_slave_pos' doesn't exist

    http://anothermysqldba.blogspot.com/2013/06/mariadb-1003-alpha-install-on-fedora-17.html MariaDB 10. ...

  3. spring历史地址

    http://repo.spring.io/release/org/springframework/spring/

  4. 20180705关于mysql binlog的解析方式

    来自:https://blog.csdn.net/u012985132/article/details/74964366/ 关系型数据库和Hadoop生态的沟通越来越密集,时效要求也越来越高.本篇就来 ...

  5. 洛谷——P2935 [USACO09JAN]最好的地方Best Spot

    P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that ...

  6. Chrome资源嗅探器应用

    http://hi.baidu.com/thankyouanyway/item/5230a49a7665848959146142 在网上转转,经常会发现一些好视频,但是往往找不到下载地址,或者需要安装 ...

  7. gem5: 使用ruby memory system中的mesh结构 出现AssertionError错误

    问题:在使用ruby memory system中的mesh结构測试时,出现例如以下错误: Traceback (most recent call last): File "<stri ...

  8. 一场BC的台前幕后

    #define BC BestCoder 一场BC的台前幕后 起源大概是这种:一个月前的BC#75结束后,AK的人非常多,于是CodeVS群里非常多人吐槽BC#75的质量,这时YJQ对KPM说:&qu ...

  9. HDU 1255 覆盖的面积(线段树+扫描线)

    题目地址:HDU 1255 这题跟面积并的方法非常像,仅仅只是须要再加一个变量. 刚開始我以为直接用那个变量即可,仅仅只是推断是否大于0改成推断是否大于1.可是后来发现了个问题,由于这个没有下放,没延 ...

  10. oninput 中文输入

    使用 input 监听 input 中文输入,会被每次输入的字母打断,下面就解决这个问题 $('input') .off() .on('input',function(){ if($(this).pr ...