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 ...
随机推荐
- Java 反射 分析类和对象
Java 反射 分析类和对象 @author ixenos 摘要:优化程序启动策略.在运行时使用反射分析类的结构和对象 优化程序启动策略 在启动时,包含main方法的类被加载.它会加载所有它需要的类. ...
- 对Big O的新的认识
对Big O的新的认识 一个问题,它有很多种算法都能实现.每种算法它的时间.空间复杂度不一样.比如: 问题1: 求最大连续子序列和的问题,可以有O(n3).O(n2).O(nlogn)和O(n)四种时 ...
- 解决VM安装VMTools后错误提示,实现文件共享
在VM里给Red Hat 9.0安装VMTools后重启,在系统启动过程中出现三处提示,分别为:第一处:Mounting local filesystem: Error: Cannot mount f ...
- HDU 2064 汉诺塔III(递归)
题目链接 Problem Description 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘 ...
- HDU 3835 R(N)(枚举)
题目链接 Problem Description We know that some positive integer x can be expressed as x=A^2+B^2(A,B are ...
- UVALive 7352 Dance Recital
题意: 有n种舞蹈要跳 每种舞蹈需要每一行字符串所对应的那些人 如果一个人连着跳两个舞蹈 那么需要一个quick change 问需要的最少quick changes是多少 思路: 假期的题 又拿出来 ...
- java的string.split()分割特殊字符时注意点
[1]单个符号作为分隔符 String address="上海|上海市|闵行区|吴中路"; String[] splitAddress=address.s ...
- B - 小Y上学记——小Y的玩偶
B - 小Y上学记——小Y的玩偶 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) ...
- 【code vs】 2780 ZZWYYQWZHZ
2780 ZZWYYQWZHZ 题目描述 Description 可爱的小管在玩吹泡泡.忽然,他想到了一种排序....... 输入描述 Input Description 第一行输入n,表示有n个数. ...
- js 技巧
用于浮窗跳转至父窗口 parent.document.location.href='/xxx/xxx.htm'; 取父窗口的元素 window.parent.$('#xxx'); 正常跳转 windo ...