Automatic Trading
Automatic Trading
A brokerage firm is interested in detecting automatic trading. They believe that a particular algorithm repeats itself; that is, it makes the same sequence of trades at a later time. The firm has identified a set of 26 key stocks that they believe are likely to be traded in concert, and they’ve encoded a series of trades as a string of letters: the letter itself indicates the stock, upper case indicates a buy, lower case indicates a sell. They want you to write a program to determine, for any two starting points, the longest sequence of identical trades from those two starting points, starting from and including those starting points as the first trade in the sequence.
Input
There will be a single test case in the input. This test case will start with a string ss on the first line, consisting solely of upper case and lower case letters (1≤length(s)≤1000001≤length(s)≤100000). On the next line will be an integer qq (1≤q≤1000001≤q≤100000), indicating the number of queries. The following qq lines each describe a query with two integers, ii and jj (0≤i<j<length(s)0≤i<j<length(s)), which represent two zero-based positions in the string.
Output
For each query, output a single integer, indicating the length of the longest sequence of trades starting at iiwhich is identical to the sequence of trades starting at jj.
| Sample Input 1 | Sample Output 1 |
|---|---|
ABABABcABABAbAbab |
4 |
| Sample Input 2 | Sample Output 2 |
|---|---|
SheSellsSeashellsByTheSeaShore |
3 |
分析:后缀数组+RMQ;(二分哈希)
i和j的最长公共前缀是i和j的后缀在字典序中的夹的height值的最小的一个;
代码:
后缀数组:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,cntA[maxn],cntB[maxn],sa[maxn],lev[maxn],height[maxn],A[maxn],B[maxn],tsa[maxn],p[maxn],st[][maxn];
char ch[maxn];
void init()
{
for(int i=;i<=n+;i++)p[i]=+p[i>>];
for(int i=;i<=n+;i++)st[][i]=height[i];
for(int i=;i<=;i++)
for(int j=;(ll)j+(1LL<<i)-<=n+;j++)
st[i][j]=min(st[i-][j],st[i-][j+(<<(i-))]);
}
int query(int a,int b)
{
if(a>b)swap(a,b);
a++;
int x=p[b-a+];
return min(st[x][a],st[x][b-(<<x)+]);
}
void solve()
{
for (int i = ; i < ; i ++) cntA[i] = ;
for (int i = ; i <= n; i ++) cntA[ch[i]] ++;
for (int i = ; i < ; i ++) cntA[i] += cntA[i - ];
for (int i = n; i; i --) sa[cntA[ch[i]] --] = i;
lev[sa[]] = ;
for (int i = ; i <= n; i ++)
{
lev[sa[i]] = lev[sa[i - ]];
if (ch[sa[i]] != ch[sa[i - ]]) lev[sa[i]] ++;
}
for (int l = ; lev[sa[n]] < n; l <<= )
{
for (int i = ; i <= n; i ++) cntA[i] = ;
for (int i = ; i <= n; i ++) cntB[i] = ;
for (int i = ; i <= n; i ++)
{
cntA[A[i] = lev[i]] ++;
cntB[B[i] = (i + l <= n) ? lev[i + l] : ] ++;
}
for (int i = ; i <= n; i ++) cntB[i] += cntB[i - ];
for (int i = n; i; i --) tsa[cntB[B[i]] --] = i;
for (int i = ; i <= n; i ++) cntA[i] += cntA[i - ];
for (int i = n; i; i --) sa[cntA[A[tsa[i]]] --] = tsa[i];
lev[sa[]] = ;
for (int i = ; i <= n; i ++)
{
lev[sa[i]] = lev[sa[i - ]];
if (A[sa[i]] != A[sa[i - ]] || B[sa[i]] != B[sa[i - ]]) lev[sa[i]] ++;
}
}
for (int i = , j = ; i <= n; i ++)
{
if (j) j --;
while (ch[i + j] == ch[sa[lev[i] - ] + j]) j ++;
height[lev[i]] = j;
}
}
int main()
{
int i,j;
while(~scanf("%s",ch+)&&strcmp(ch+,"*")!=)
{
n=strlen(ch+);
solve();
init();
scanf("%d",&m);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
a++,b++;
printf("%d\n",query(lev[a],lev[b]));
}
}
//system("Pause");
return ;
}
二分哈希:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=1e5+;
const int mo=;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,len;
unsigned ll h[maxn],xp[maxn];
char a[maxn];
void init()
{
xp[]=;
for(int i=;i<=maxn-;i++)xp[i]=xp[i-]*mo;
}
bool check(int p,int b,int c)
{
return h[b]-h[b+p]*xp[p]==h[c]-h[c+p]*xp[p];
}
int main()
{
int i,j;
init();
while(~scanf("%s",a)&&strcmp(a,"*")!=)
{
len=strlen(a);
h[len]=;
for(i=len-;i>=;i--)
{
h[i]=h[i+]*mo+(a[i]>='a'?a[i]-'a':a[i]-'A'+);
}
scanf("%d",&n);
while(n--)
{
int b,c;
scanf("%d%d",&b,&c);
int l=,r=len-max(b,c),ans;
while(l<=r)
{
int mid=l+r>>;
if(check(mid,b,c))ans=mid,l=mid+;
else r=mid-;
}
printf("%d\n",ans);
}
}
//system("Pause");
return ;
}
Automatic Trading的更多相关文章
- APIPA(Automatic Private IP Addressing,自动专用IP寻址)
APIPA APIPA(Automatic Private IP Addressing,自动专用IP寻址),是一个DHCP故障转移机制.当DHCP服务器出故障时, APIPA在169.254.0.1到 ...
- [SharePoint 2013] Automatic deployment script
Implement automatic deployment through windows task. Add-PsSnapin Microsoft.SharePoint.PowerShell $t ...
- JSONKit does not support Objective-C Automatic Reference Counting(ARC) / ARC forbids Objective-C objects in struct
当我们在使用JSONKit处理数据时,直接将文件拉进项目往往会报这两个错“JSONKit does not support Objective-C Automatic Reference Coun ...
- iOS开发 JSonKit does not support Objective-C Automatic Reference Counting(ARC)
有使用JSonKit的朋友,如果遇到“JSonKit does not support Objective-C Automatic Reference Counting(ARC)”这种情况,可参照如下 ...
- High Frequency Trading (整理中...)
什么是高频交易系统 1 交易指令完全是由电脑发送,对市场数据的响应延时在微秒级2 系统有专用的软硬件组成,研发时需要大量的计算机专家级的工作3 系统的硬件需要放在离交易所主机很近的位置,所谓co-lo ...
- [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode
转载地址:http://choijing.iteye.com/blog/1860761 后来发现是编译选项的问题: 1.点击工程名 打开编译选项 2.在编译选项中,选择Bulid Settin ...
- [转]关于NSAutoreleasePool' is unavailable: not available in automatic reference counting mode的解决方法
转载地址:http://blog.csdn.net/xbl1986/article/details/7216668 Xcode是Version 4.2 Build 4D151a 根据Objective ...
- 《Automatic Face Classification of Cushing’s Syndrome in Women – A Novel Screening Approach》学习笔记
<针对女性库欣综合征患者的自动面部分类-一种新颖的筛查方法> Abstract 目的:库兴氏综合征对身体造成相当大的伤害如果不及时治疗,还经常是诊断的时间太长.在这项研究中,我们旨在测试面 ...
- uva 11054 wine trading in gergovia (归纳【好吧这是我自己起的名字】)——yhx
As you may know from the comic \Asterix and the Chieftain's Shield", Gergovia consists of one s ...
随机推荐
- mybatis 批量插入值的sql
<insert id="insertAwardPic" useGeneratedKeys="true" parameterType="java. ...
- bzoj1417: Pku3156 Interconnect
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1417 1417: Pku3156 Interconnect Time Limit: 10 ...
- 手动编译生成apk
转载一篇介绍如何去手动编译生成apk的文章: http://jojol-zhou.iteye.com/blog/729254
- 解决time_wait过多的问题
#netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’ LAST_ACK 14SYN_RECV 348ESTABLI ...
- 201312月CCF-2,ISBN号码分析
明天要考CCF啦,偶还是很紧张的.最近看了数据结构,今天才开始上机练习,对,我就是这么懒..废话不多说,我写这篇文章主要是分析CCF编程的小窍门,因为在网上没找到,所以我决定自力更生丰衣足食.!!!! ...
- iOS中的触摸事件,手势识别,摇晃事件等
在iOS中,事件可以划分为以下几类: 1.触摸事件:通过触摸,手势进行触发(手指点击.缩放等) 2.运动事件:通过加速器触发(例如手机晃动) 3.远程控制事件:通过其他远程设备触发(例如耳机控制按钮) ...
- WCF部署在IIS上
WCF部署在IIS上 环境vs2010,WCF应用程序.如何将WCF部署在IIS上. 第一步:右键点击项目,选择生成部署包. 第二步:在你项目所在的文件目录下找到Package文件夹,这就是我们的部署 ...
- java 图形界面 mvc模式控制
使用模型-视图-控件结构来开发GUI程序. 下面的程序演示了MVC模式开发的java程序. 其中CircleModel为模型,包含了圆的半径,是否填充,等属性. CircleView为视图,显示这个圆 ...
- 一个有用的shell脚本
#!/bin/bash #if [ $1 -eq null ]; then # echo "please input params1!" # exit #fi #if [ $2 - ...
- Oracle结构知识学习+部分函数实例
一 Oracle的结构1 实例(instance) 是内存和后台进程的集合, 数据库是数据的物理储存;实例和数据库的关系是一对多的关系;2 多个实例同时驱动一个'数据库'的架构 叫集群(RAC)3 O ...