BZOJ2083:[POI] TES-Intelligence Test
给定一个数串,和m个小数串,问这些小串都是不是大数字串的子序列
思路
用vector容器储存每一个数字的位置,记last保存当前序列check的值在母序列中的合法位置,二分查找ai在母序列中的大于等于last的值,如果能找到这样的位置,继续check,如果找不到,即该序列不合法
时间复杂度O(∑L+n)
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 1000000+10;
vector <int> v[MAXN];
int n,m,a,k;
void Read(int &x)
{
int in=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(f=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9') {in=in*10+ch-'0';ch=getchar();}
x=in*f;
}
int main()
{
Read(n);
for(int i=1;i<=n;i++)
{
Read(a);
v[a].push_back(i);
}
Read(m);
for(int i=1;i<=m;i++)
{
Read(k);
bool flag=1;int last=0;
vector<int>::iterator node;
for(int j=1;j<=k;j++)
{
Read(a);
if(flag)
{
node = upper_bound(v[a].begin(),v[a].end(),last);
if(node == v[a].end() ) flag=0;
else last = *node;
}
}
if(flag) puts("TAK");
else puts("NIE");
}
return 0;
}