[2020牛客NOIP赛前集训营-提高组(第二场)] 补题

C.前缀

模拟。

Code(暂时还没打高精)

Talk is cheap.Show me the Code.

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define next nxt
#define int long long
using namespace std;
inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
    return x * f;
}
const int N = 1e4+7, mod = 998244353;
int n,m;
int next[N<<1][26];
string s;
vector<int> w[26];
int Get(int x,int pos) {
	int l = 0, r = w[x].size()-1, mid, ans = 0;
	while(l <= r) {
		mid = (l + r) >> 1;
		if(w[x][mid] >= pos) {
			ans = mid; r = mid - 1;
		} else l = mid + 1;
	}
	return ans;
}
signed main()
{
	cin >> s; n = s.length(); s = s + s;
	for(int j=0;j<26;++j) next[2*n][j] = INF;
	for(int i=2*n-1;i>=0;--i) {
		for(int j=0;j<26;++j) next[i][j] = next[i+1][j];
		next[i][s[i]-'a'] = i;
	}
	for(int i=0;i<n;++i) w[s[i]-'a'].push_back(i);
	m = read();
	while(m--) {
		string t; cin >> t;
		int lt = t.length(), pos = 0, ans = 0;
		if(t[0] == '*') pos = 0, ans = 1;
		else {
			int npos = next[0][t[0]-'a'];
			ans = npos + 1, pos = npos;
			if(npos == INF) ans = -1;
		}
		if(ans == -1) {
			printf("%lld\n",ans); continue;
		}
		for(int i=1;i<lt;++i) {
			if(t[i] == '*') {
				pos = (pos + 1) % n;
				ans = (ans + 1) % mod;
			} else if(t[i]>='a'&&t[i]<='z') {
				int x = t[i] - 'a';
				int npos = next[pos+1][x];
				if(npos == INF) {
					ans = -1; break;
				}
				ans = (ans + npos-pos) % mod;
				pos = npos % n;
			} else {
				if(t[i-1] == '*') {
					int y = 0;
					while(i<lt && t[i]>='0'&&t[i]<='9') {
						y = (y<<3) + (y<<1) + (t[i]^48); ++i;
					}
					--y; --i;
					ans = (ans + y) % mod;
					pos = (pos + y) % n;
				} else {
					int x = t[i-1] - 'a', y = 0;
					while(i<lt && t[i]>='0'&&t[i]<='9') {
						y = (y<<3) + (y<<1) + (t[i]^48); ++i;
					}
					--y; --i;
					int inw = Get(x,pos), sz = w[x].size(), yu = y%sz;
					ans = (ans + n*(y/sz)%mod) % mod;
					if(inw+yu >= sz) {
						pos = w[x][(inw+yu)%sz];
						ans = (ans + n-w[x][inw] + pos) % mod;
					} else {
						pos = w[x][inw+yu];
						ans = (ans + pos-w[x][inw]) % mod;
					}
				}
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*
wznysgxmnopecwoejwblabfnuyulfa
3
c10652o23656a24242
si23844z
k26389ze2152


1038000
-1
-1
*/
posted @ 2020-10-21 11:18  基地AI  阅读(126)  评论(0编辑  收藏  举报