思路:

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. codevs1213 解的个数

    题目描述 Description 已知整数x,y满足如下面的条件: ax+by+c = 0 p<=x<=q r<=y<=s 求满足这些条件的x,y的个数. 输入描述 Input ...

  2. codeforces gym 100357 J (网络流)

    题目大意 有n种物品,m种建筑,p个人. n,m,p∈[1,20] 每种建筑需要若干个若干种物品来建造.每个人打算建造一种建筑,拥有一些物品. 主角需要通过交易来建造自己的建筑,交易的前提是对方用多余 ...

  3. Ubuntu 16.04监控网络带宽软件

    1.系统监控器已经自带了默认的监控功能,包括CPU.内存.带宽.进程等,但带宽的一些详情不能显示出来. 2.使用top命令进行监控进程 sudo top 3.安装Indicator-sysmonito ...

  4. Caffe——清晰高效的深度学习(Deep Learning)框架

    Caffe(http://caffe.berkeleyvision.org/)是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的贾扬清(http://daggerfs.com/ ...

  5. nginx源代码分析--框架设计 &amp; master-worker进程模型

    Nginx的框架设计-进程模型 在这之前,我们首先澄清几点事实: nginx作为一个高性能server的特点.事实上这也是全部的高性能server的特点,依赖epoll系统调用的高效(高效是相对sel ...

  6. Java大数练习第二弹

    hdu1250 水题 题目链接:pid=1250">http://acm.hdu.edu.cn/showproblem.php?pid=1250 import java.util.*; ...

  7. Apache Kafka-0.8.1.1源代码编译

    作者:过往记忆 | 新浪微博:左手牵右手TEL | 能够转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明博客地址:http://www.iteblog.com/文章标题:<Apac ...

  8. Codeforces Round #272 (Div. 2)C. Dreamoon and Sums 数学推公式

    C. Dreamoon and Sums   Dreamoon loves summing up something for no reason. One day he obtains two int ...

  9. ASP.NET_SessionId vs .ASPXAUTH why do we need both of them?

    https://stackoverflow.com/questions/23758704/asp-net-sessionid-vs-aspxauth-why-do-we-need-both-of-th ...

  10. C# 数据库访问

    C# 数据库访问 分类: C#学习笔记2011-07-05 11:26 515人阅读 评论(0) 收藏 举报 数据库c#datasettextboxcommandexception   目录(?)[+ ...