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
3
0 2
1 6
0 7
4
0
5
Sample Input 2 Sample Output 2
SheSellsSeashellsByTheSeaShore
4
8 22
1 20
8 25
0 1
3
4
1
0

分析:后缀数组+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的更多相关文章

  1. APIPA(Automatic Private IP Addressing,自动专用IP寻址)

    APIPA APIPA(Automatic Private IP Addressing,自动专用IP寻址),是一个DHCP故障转移机制.当DHCP服务器出故障时, APIPA在169.254.0.1到 ...

  2. [SharePoint 2013] Automatic deployment script

    Implement automatic deployment through windows task. Add-PsSnapin Microsoft.SharePoint.PowerShell $t ...

  3. 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 ...

  4. iOS开发 JSonKit does not support Objective-C Automatic Reference Counting(ARC)

    有使用JSonKit的朋友,如果遇到“JSonKit does not support Objective-C Automatic Reference Counting(ARC)”这种情况,可参照如下 ...

  5. High Frequency Trading (整理中...)

    什么是高频交易系统 1 交易指令完全是由电脑发送,对市场数据的响应延时在微秒级2 系统有专用的软硬件组成,研发时需要大量的计算机专家级的工作3 系统的硬件需要放在离交易所主机很近的位置,所谓co-lo ...

  6. [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode

    转载地址:http://choijing.iteye.com/blog/1860761   后来发现是编译选项的问题:   1.点击工程名 打开编译选项 2.在编译选项中,选择Bulid Settin ...

  7. [转]关于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 ...

  8. 《Automatic Face Classification of Cushing’s Syndrome in Women – A Novel Screening Approach》学习笔记

    <针对女性库欣综合征患者的自动面部分类-一种新颖的筛查方法> Abstract 目的:库兴氏综合征对身体造成相当大的伤害如果不及时治疗,还经常是诊断的时间太长.在这项研究中,我们旨在测试面 ...

  9. 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 ...

随机推荐

  1. CSS3秘笈复习:第九章&第十章

    第九章 1.和链接有关的伪类: (1):link,未访问过的链接 (2):visited,已访问过的链接 (3):hover,鼠标悬停链接 (4):active,单击链接时 这四种方式一定要严格按上面 ...

  2. asp.net javascript客户端调用服务器端方法

    如何用js调用服务器端方法.首先服务器端方法的格式如下 [System.Web.Services.WebMethod]        public static void serverMethod(s ...

  3. Markdown 基础

    How to use Markdown H1 text. H2 text. H3 text. H4 text. H5 text. H6 text. Text This is italic text. ...

  4. AOE 网络

    1.定义 如果在无向环的带权有向图中 - 用有向边表示一个工程中的活动 - 用边上的权值表示活动的持续时间 - 用顶点表示事件 则这样的有向图叫做用边表示活动的网络,简称AOE网络 AOE在工程方面非 ...

  5. Traffic Ccontrol(流量控制)

    linux有一个成熟的带宽供给系统,称为Traffic Control(流量控制).这个系统支持各种方式进行分类.排序.共享和限制出入流量. 一.基础知识 让ip显示我们的链路 ip link  li ...

  6. HDU 2102 A计划(DFS)

    题目链接 Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主 ...

  7. 防暴力破解 Fail2Ban之python

    fai2ban的介绍 fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽),如:当有人在试探你的SSH.SMTP.FTP密码,只要 ...

  8. Openjudge-计算概论(A)-奇数求和

    描述: 计算非负整数 m 到 n(包括m 和 n )之间的所有奇数的和,其中,m 不大于 n,且n 不大于300.例如 m=3, n=12, 其和则为:3+5+7+9+11=35. 输入两个数 m 和 ...

  9. c#设计模式-单例模式(面试题)

    c#设计模式-单例模式 单例模式三种写法: 第一种最简单,但没有考虑线程安全,在多线程时可能会出问题, public class Singleton { private static Singleto ...

  10. SVN通过域名连不上服务器地址(svn: E175002: OPTIONS request failed on '/svn/yx-SVN-Server' Connection refused: connect)

    用域名直连就连不上,如果换成了ip直连就可以连接上去了 https://yx-server01/svn/yx-SVN-Server 换为了 https://192.168.188.208/svn/yx ...