cheat (CTSC2012)

78 篇文章 2 订阅
55 篇文章 0 订阅

http://www.lydsy.com/JudgeOnline/problem.php?id=2806

用特殊字符分隔每个“标准作文”,建SAM,对于每个询问串,先把询问串在SAM里跑一遍求出每个位置在“标准作文”的子串中出现的最长后缀,二分询问串长,然后单调队列优化dp用判别。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef LL TY;
typedef long double LF;

const int MAXN(2200010);
const int MAXM(110);
const int MAXE(10010);
const int MAXK(6);
const int HSIZE(31313);
const int SIGMA_SIZE(3);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
const int MOD(20100403);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> T ABS(const T &a){return a < 0? -a: a;}
;
struct SAM
{
	struct NODE
	{
		int mx;
		NODE *f, *ch[SIGMA_SIZE];
	};
	NODE pool[MAXN << 1];
	NODE *root, *last;
	int size;
	void init()
	{
		root = last = pool;
		root->f = 0;
		root->mx = 0;
		memset(root->ch, 0, sizeof(root->ch));
		size = 1;
	}
	NODE *newnode(int tl)
	{
		pool[size].mx = tl;
		memset(pool[size].ch, 0, sizeof(pool[size].ch));
		return pool+size++;
	}
	void extend(int id)
	{
		NODE *p = last, *np = newnode(last->mx+1);
		last = np;
		while(p && p->ch[id] == 0)
			p->ch[id] = np, p = p->f;
		if(p == 0)
			np->f = root;
		else
		{
			NODE *q = p->ch[id];
			if(p->mx+1 == q->mx)
				np->f = q;
			else
			{
				NODE *nq = newnode(p->mx+1);
				memcpy(nq->ch, q->ch, sizeof(nq->ch));
				nq->f = q->f;
				q->f = np->f = nq;
				while(p && p->ch[id] == q)
					p->ch[id] = nq, p = p->f;
			}
		}
	}
} sam;

int L[MAXN], len;
int table[MAXN], que[MAXN], front, back;
char str[MAXN];

inline int getval(int ti){return table[ti]-ti;}

bool check(int value)
{
	front = 0, back = -1;
	for(int i = 0; i < value; ++i)
		table[i] = 0;
	for(int i = value; i <= len; ++i)
	{
		table[i] = table[i-1];
		int ti = i-value;
		while(front <= back && getval(ti) >= getval(que[back])) --back;
		que[++back] = ti;
		while(front <= back && i-que[front] > L[i]) ++front;
		if(front <= back) checkmax(table[i], i+getval(que[front]));
	}
	return table[len]*100 >= len*90;
}

int main()
{
	int N, M;
	while(~scanf("%d%d", &N, &M))
	{
		sam.init();
		for(int i = 0; i < M; ++i)
		{
			scanf("%s", str);
			if(i) sam.extend(2);
			for(char *sp = str; *sp; ++sp)
				sam.extend(*sp-'0');
		}
		for(int i = 0; i < N; ++i)
		{
			scanf("%s", str);
			SAM::NODE *p = sam.root;
			int l = 0;
			len = 0;
			for(char *sp = str; *sp; ++sp, ++len)
			{
				int id = *sp-'0';
				if(p->ch[id])
				{
					++l;
					p = p->ch[id];
				}
				else
				{
					while(p && !p->ch[id]) p = p->f;
					if(p)
					{
						l = p->mx+1;
						p = p->ch[id];
					}
					else
					{
						p = sam.root;
						l = 0;
					}
				}
				L[len+1] = l;
			}
			int lo = 1, hi = len+1;
			while(lo < hi)
			{
				int m = (lo+hi) >> 1;
				if(check(m))
					lo = m+1;
				else
					hi = m;
			}
			printf("%d\n", lo-1);
		}
	}
	return 0;	
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值