[vijos1005]超长数字串

超长数字串

背景

George很喜欢数学,尤其是算数数系列。

描述

他最喜欢的是数字的无穷序列,结果是把所有的自然数按升序排列。这个序列开始是: 1234567891011121314... 我们叫序列 S。然后 S[1] = 1, S[2] = 2, ... , S[10] = 1, S[11] = 0, ... , 以此类推。
George 现有一个数字系列 A ,他想知道在S中最早出现的位置。帮助他解决这个难题。

输入格式

输入文件包含 A - 给出的数字系列。位数不超过 200。没有空格。

输出格式

输出一个整数。- 最小的 k ,使 A[1] = S[k], A[2] = S[k+1], ... A[len(A)] = S[k + len(A) -1], len(A) 表示 A 的长度。

样例1

样例输入1

101

样例输出1

10

限制

每个测试点1s

来源

Ural State University Problem Archive

题解

部分分

循环队列+字符串hash
无限循环,依次计算各字串的哈希值,与串A进行匹配,找到为止,然后,略

正解

暂无

源代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL base=10000;

char s[300],c[]={'0','1','2','3','4','5','6','7','8','9'};
char t[1000];int size;
ULL Hash,hashh,hasht,dt=1,ans=1,head=1,tail;
ULL Tp[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,100000000000000};
int main(){
	scanf("%s",s);int len=strlen(s);
	for(int i=0;i<len;i++)
		Hash=Hash*base+s[i],dt*=base;
	ULL num=0;bool first=true;
	while(true){
		while(size<=len){
			num++;
			int pow=0;
			while(Tp[pow]<=num)pow++;
			while(pow){
				pow--;
				int n=(num/Tp[pow])%10;
				tail=(tail+1)%512;
				t[tail]=c[n];size++;
			}
		}
		if(first){
			for(int i=1;i<=len;i++)
				hasht=hasht*base+t[i];
			if(hasht==Hash)break;
			first=false;
		}
		hashh=hashh*base+t[head];
		hasht=hasht*base+t[head+len];
		head=(head+1)%512;ans++;size--;
		if(Hash==hasht-hashh*dt)break;
	}
	printf("%lld",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/Anoxiacxy/p/7041214.html