思路:

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. CodeForcesGym 100753A A Journey to Greece

    A Journey to Greece Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeFor ...

  2. [K/3Cloud]关于数据库sa密码更改,管理中心登录不上的问题。

    有时候可能应为别的原因可能一不小心更改了数据库的密码,导致K/3 Cloud管理中心和单据打不开. 这个时候其实只要在注册一下就能解决了,在浏览器中输入http://192.168.25.35:800 ...

  3. C# 解决EXCEL单元格合并,行高无法自适应问题

    解决方法:根据单元格内容长度,设置单元格所在行的行高 public static float getExcelCellAutoHeight(string strtest, float fontCoun ...

  4. ScrollView双击图片定点放大

    直接先说原理吧--原理:利用了scrollview的回调函数(如下)以及scrollview自己内部的一些缩放规则(其实我也还没弄清楚具体scrollview干了什么事),只是知道了它可以怎么做-_- ...

  5. 关于static静态块的使用和static list的使用

    public class CorsConfiguration { /** * Wildcard representing <em>all</em> origins, metho ...

  6. MQTT---HiveMQ源代码具体解释(一)概览

    源博客地址:http://blog.csdn.net/pipinet123 MQTT交流群:221405150 面向群体 想自己实现MQTT Broker的朋友 对现有开源的MQTT Broker或多 ...

  7. [Dart] Understand Variables and Constants in Dart

    In this lesson, we will look at how to create variables and constants. These are containers that sto ...

  8. 自己动手写reg注册表文件

    自己动手写reg注册表文件 2015-01-12 20:23 1161人阅读 评论(1) 收藏 举报  分类: 玩转Windows应用层编程(12)  版权声明:本文为博主原创文章,未经博主允许不得转 ...

  9. WCF问题集锦:ReadResponse failed: The server did not return a complete response for this request.

    今日.对代码进行单元測试时.发现方法GetAllSupplyTypes报例如以下错误: [Fiddler] ReadResponse() failed: The server did not retu ...

  10. 0x0118消息就是WM_SYSTIMER

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/c0f9bac9-d211-4b8b-ba99-f5a0ed0d2e0a/what-is-w ...