QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#114227#6635. Strange KeyboardjeffqiTL 445ms34484kbC++232.6kb2023-06-21 17:09:492023-06-21 17:09:50

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-21 17:09:50]
  • 评测
  • 测评结果:TL
  • 用时:445ms
  • 内存:34484kb
  • [2023-06-21 17:09:49]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define umap unordered_map
using namespace std;
namespace qiqi {
	const int inf = 1e9,P[2] = {(int)1e9+7,(int)1e9+9},B = 27;
	struct SH {
		int v[2];
		SH():v{0,0} {}
		SH(int a,int b):v{a,b} {}
		SH friend operator + (SH x,int k) {
			SH y; for (int i = 0; i < 2; i++) {
				y.v[i] = (1ll*B*x.v[i]+k+1)%P[i];
			}
			return y;
		}
		bool friend operator == (SH x,SH y) {
			for (int i = 0; i < 2; i++) {
				if (x.v[i] != y.v[i]) {
					return 0;
				}
			}
			return 1;
		}
		ll h() {
			return 1ll*v[0]*P[1]+v[1];
		}
	};
	mt19937_64 rng(random_device{}());
	struct Hash_table {
		static const int V = 1e6+3;
		const ll C = rng();
		struct Node {
			SH key; int val;
			Node(SH x,int k):key(x),val(k) {}
		};
		vector<vector<Node>> vec;
		Hash_table() {
			vec.assign(V,vector<Node>());
		}
		void upd(SH x,int k) {
			int p = ((x.h()^C)%V+V)%V;
			for (auto& [y,v]:vec[p]) {
				if (x == y) {
					v = min(v,k);
					return;
				}
			}
			vec[p].eb(x,k);
		}
		int qry(SH x) {
			int p = ((x.h()^C)%V+V)%V;
			for (auto [y,v]:vec[p]) {
				if (x == y) {
					return v;
				}
			}
			return inf;
		}
	};
	void main() {
		int n,m;
		cin >> n >> m;
		vi c(m,inf); c[0] = 0;
		vector<vector<SH>> a(n);
		for (int i = 0; i < n; i++) {
			string s; cin >> s;
			int k = sz(s);
			c[k%m] = min(c[k%m],k/m+1);
			a[i].assign(k+1,SH());
			for (int j = 0; j < k; j++) {
				a[i][j+1] = a[i][j]+(s[j]-'a');
			}
		}
		for (int i = 1; i < m; i++) {
			int g = __gcd(i,m);
			for (int j = 0; j < g; j++) {
				for (int k = 0,x = i,y = (x+i)%m; k <= 2*m/g; k++,x = y,y = (y+i)%m) {
					c[y] = min(c[y],c[x]+c[i]+(y < x));
				}
			}
		}
		Hash_table hst;
		for (int i = 0; i < n; i++) {
			int x = sz(a[i]);
			for (int j = 1; j < x; j++) {
				int y = c[(m-(x-j-1)%m)%m]+(x-j-1+m-1)/m+1;
				if (y != inf) {
					hst.upd(a[i][j],y);
				}
			}
		}
		string s; cin >> s;
		n = sz(s);
		vi f(n+1,inf); f[0] = 0;
		for (int i = 0; i < n; i++) {
			SH x;
			for (int j = i; j < n; j++) {
				x = x+(s[j]-'a');
				f[j+1] = min(f[j+1],f[i]+hst.qry(x));
			}
		}
		if (f[n] == inf) {
			cout << "-1\n";
			return;
		}
		cout << f[n] << '\n';
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int T; cin >> T;
	while (T--) qiqi::main();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 26888kb

input:

2
2 3
defgh
abc
abcde
1 1
a
b

output:

3
-1

result:

ok 2 number(s): "3 -1"

Test #2:

score: 0
Accepted
time: 445ms
memory: 34484kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: -100
Time Limit Exceeded

input:

10
446 4905
afigcjhcgagabbiccehjcjajigghgbjjadccicghggijjdfeciaccgheedjdhgfjdfdbgidbbdjaiehhceeehchhabhaideggjbjajgfgicfdggahhbjgdebccbgbiedhehaebdccdfdffaacjcfbgjeegbahhbgcdjigijajheidchbddicehhhjbeiaajgedhdcjiefdgdbjjfaegheeidieheecaicciaajbabiidcecefgiicccdidegeica
afigcjhcgagabbiccehjcjajigghgbj...

output:


result: