QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#498978#6635. Strange Keyboardsalvator_nosterAC ✓1241ms150184kbC++205.2kb2024-07-30 22:31:442024-07-30 22:31:45

Judging History

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

  • [2024-07-30 22:31:45]
  • 评测
  • 测评结果:AC
  • 用时:1241ms
  • 内存:150184kb
  • [2024-07-30 22:31:44]
  • 提交

answer

#include <bits/stdc++.h>

template <class T>
inline int qlog(T a) {
    if (!a) return 0;
	double x = a;
	return ((*(long long*) & x) >> 52 & 2047) - 1023;
}

#define cin Mizuhashi
#define cout Parsee
#define endl '\n'

class Reader{
	private:
	    static const int BUF_SIZE = 1 << 22;
	    char BUF_R[BUF_SIZE], *csy1, *csy2;
	    #ifndef _LOCAL_RUNNING
		    #define GC (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1 ++)
		#else
		    char cur;
		    #define GC (cur = getchar())
		#endif
	    #define IL inline
		
    public:
        IL bool eof() {
            #ifndef _LOCAL_RUNNING
                return csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2);
            #else
                return cur == EOF;
            #endif
        }
        template <class Ty>
        IL Reader& operator >> (Ty &t) {
            int u = 0;
            char c = GC;
	    	for (t = 0; c < 48 || c > 57; c = GC)
                if (c == EOF) break;
                else if (c == '-') u = 1;
	    	for ( ; c > 47 && c < 58; c = GC) t = (t << 1) + (t << 3) + (c ^ 48);
	    	t = u ? -t : t; return *this;
        }
    	IL Reader& operator >> (double &t) {
            int tmp, u = 0; char c = GC;
	    	for (tmp = 0; c < 48 || c > 57; c = GC)
                if (c == EOF) break;
                else if (c == '-') u = 1;
	    	for ( ; c > 47 && c < 58; c = GC) tmp = (tmp << 1) + (tmp << 3) + (c ^ 48);
	    	t = (tmp = u ? -tmp : tmp);
    	    if (c == '.') {
    	        double x = 1;
    	        for (c = GC; c > 47 && c < 58; c = GC) t += (x /= 10) * (c ^ 48);
    	    }
    	    return *this;
    	}
    	IL Reader& operator >> (char *s) {
			char c = GC;
			for (*s = 0; c < 33; c = GC) if (c == EOF) return *this;
			for ( ; c > 32; c = GC) *s ++ = c;
			*s = 0; return *this;
		}
        IL Reader& operator >> (char &c) {
			for (c = GC; c < 33; c = GC) if (c == EOF) return *this;
            return *this;
        }
}cin;
class Writer{
	private:
	    static const int BUF_SIZE = 1 << 22;
	    char BUF_W[BUF_SIZE], *csy;
	    #define IL inline
		inline void WC(const char c) {
			if (csy - BUF_W == BUF_SIZE) fwrite(BUF_W, 1, BUF_SIZE, stdout), csy = BUF_W;
			*csy ++ = c;
		}
	
	public:
		Writer() : csy(BUF_W) {}
		~ Writer() {fwrite(BUF_W, 1, csy - BUF_W, stdout);}
		IL void flush() {fwrite(BUF_W, 1, csy - BUF_W, stdout); csy = BUF_W;}
		template <class Ty>
		IL Writer& operator << (Ty x) {
		    static int sta[32], top;
			if (x < 0) {
				WC('-');
                do sta[top ++] = - (x % 10); while (x /= 10);
			}else do sta[top ++] = x % 10; while (x /= 10);
			while (top) WC(sta[-- top] ^ 48);
			return *this;
		}
		IL Writer& operator << (const char &c) {WC(c); return *this;}
		IL Writer& operator << (const char *s) {while (*s) WC(*s ++); return *this;}
		IL Writer& operator << (char *s) {while (*s) WC(*s ++); return *this;}
}cout;

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef vector <int> vi;

const int MAX_N = 5000 + 5;
const int MAX_M = 1000000 + 5;
const int INF32 = 0x3f3f3f3f;
const ll INF64 = 1e18;
const int P = 998244353;

int N, K, minc[MAX_N];
string s[MAX_M];
char t[MAX_N], vis[MAX_N];
ll dis[MAX_N], f[MAX_N];
struct Node{
	int son[26];
	ll val;
	
	Node () {
		val = INF64;
		memset(son, 0, sizeof son);
	}
	
	void clear() {
		val = INF64;
		memset(son, 0, sizeof son);
	}
}node[MAX_M];
int tot;

void ins(const string &s) {
	int n = s.length(), u = 0;
	for (int i = 0; i < n; i ++) {
		ll cost = dis[K - (n - i - 1) % K] + (n - i + K - 2) / K + 1;
		
		int &v = node[u].son[s[i] - 'a']; v = v ? v : ++ tot;
		node[v].val = min(node[v].val, cost); u = v;
	}
}

void query(int p, int n) {
	int u = 0;
	for (int i = p + 1; i <= n; i ++) {
		u = node[u].son[t[i] - 'a'];
		if (!u) return ;
		f[i] = min(f[i], f[p] + node[u].val);
	}
}

void solve() {
	cin >> N >> K;
	for (int i = 1; i < K; i ++) {
		minc[i] = INF32;
		vis[i] = 0;
		dis[i] = INF64;
	}
	vi edge(0);
	set <int> mark;
	for (int i = 1; i <= N; i ++) {
		static char str[MAX_M];
		cin >> str; s[i] = str;
		int tmp = s[i].length() % K;
		minc[tmp] = min(minc[tmp], (int)s[i].length() / K + 1);
		if (mark.find(tmp) == mark.end()) {
			mark.insert(tmp);
			edge.push_back(tmp);
		}
	}
	cin >> t + 1;
	vis[0] = 0;
	for (int round = 0; round < K; round ++) {
		int p = -1;
		for (int i = 0; i < K; i ++) {
			if (!vis[i] && (p < 0 || dis[i] < dis[p])) p = i;
		}
		if (dis[p] >= INF64) break;
		vis[p] = 1;
		for (auto i : edge) {
			if (p + i < K) {
				dis[p + i] = min(dis[p + i], dis[p] + minc[i]);
			}else {
				dis[p + i - K] = min(dis[p + i - K], dis[p] + minc[i] + 1);
			}
		}
	}
	dis[K] = 0;
	for (int i = 0; i <= tot; i ++) node[i].clear();
	tot = 0;
	for (int i = 1; i <= N; i ++) ins(s[i]);
	int M = strlen(t + 1);
	
	for (int i = 1; i <= M; i ++) f[i] = INF64;
	for (int i = 0; i < M; i ++) query(i, M);
	
	if (f[M] < INF64) cout << f[M] << endl;
	else cout << -1 << endl;
}

int main() {
    int T = 1;
    cin >> T;
    while (T --) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 21ms
memory: 147036kb

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: 39ms
memory: 149944kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 150ms
memory: 148976kb

input:

10
446 4905
afigcjhcgagabbiccehjcjajigghgbjjadccicghggijjdfeciaccgheedjdhgfjdfdbgidbbdjaiehhceeehchhabhaideggjbjajgfgicfdggahhbjgdebccbgbiedhehaebdccdfdffaacjcfbgjeegbahhbgcdjigijajheidchbddicehhhjbeiaajgedhdcjiefdgdbjjfaegheeidieheecaicciaajbabiidcecefgiicccdidegeica
afigcjhcgagabbiccehjcjajigghgbj...

output:

3
2
2
11
6
5
1
1
1
1

result:

ok 10 numbers

Test #4:

score: 0
Accepted
time: 1241ms
memory: 148588kb

input:

100
140 4879
baabaababbababbaabababaaababbbabbbbbbabbababbbabbbbabbbbbbaabbbbbbbbabaabbbaabaabbbaabbabaabaabbbabbbababbbaabbabaaaaabbaaabbbabb
baa
baabaababbababbaabababaaababbbabbbbbbabbab
baabaababbababbaabababaaabab
baabaababbababbaabababaaababbbabbb
baabaababbababbaabababaaababbbabbbbbbabbababbb...

output:

1
1
1
1
3
1
1
1
1
1
1
3
2
1
1
1
2
1
1
2
1
1
1
1
1
1
1
1
1
4
3
2
1
2
1
1
1
1
1
2
1
1
1
3
1
1
1
2
1
1
1
2
3
1
1
1
2
1
1
1
1
1
1
1
1
3
2
3
1
3
1
1
2
1
2
3
2
1
1
1
3
2
1
2
1
1
1
1
1
1
1
1
1
1
1
1
2
1
4
1

result:

ok 100 numbers

Test #5:

score: 0
Accepted
time: 83ms
memory: 148364kb

input:

1
7 4864
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

205

result:

ok 1 number(s): "205"

Test #6:

score: 0
Accepted
time: 164ms
memory: 150140kb

input:

10
7 4923
baaabbabbbabbbbbabaabaabababbabbaaaabbaaabbbbabbaaababaaaaabaaabbabbbabbaaaabbaabbbaabbaaababaaababbabaababbaababbabaaabaabbaaabaaaababbabbabaabaabaabaabbbbaabaabbbaababbabbabaaaabbbaabbbaaaabaaaaababbaabaaaaababbbbaaaabbbaababbaabbabaabaaaaababaababaabaaaaabababababbabbbabababaaabaababbaa...

output:

4287
228
3671
549
129
372
475
534
336
288

result:

ok 10 numbers

Test #7:

score: 0
Accepted
time: 1224ms
memory: 148888kb

input:

100
7 4807
abbababaababbaabbabbaabaababbaaababaabaaabbaaaabababbbaabbaaabababbaabaabbaaaaabbbbaabbbaaabbbbabaabbbaaaaabbbaabbaaaabbaaababbaaabbbbabaabbababababbbabaaabaaaabbbbabbabbbbbaabaaabaababbabaaabbaabbabbabaaababbbabbabbbaababaabaaaabaaabbbbabbaabaababbbabbbbaaaabbabbbaabbaabbbbb
aaaaababaaab...

output:

45
32
11
4
2475
132
50
330
20
6
99
25
126
6
4
14
74
108
208
11
5
67
166
2822
178
1307
548
92
386
493
279
2415
255
262
567
215
46
113
31
651
17
4
8
21
12
100
69
152
15
55
521
146
11
13
181
-1
442
1839
2
5
31
26
122
696
280
77
1
839
11
273
7
178
421
228
6
6
82
116
1
-1
293
519
5
160
15
126
13
31
619
4...

result:

ok 100 numbers

Test #8:

score: 0
Accepted
time: 32ms
memory: 150184kb

input:

1
1 5000
abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

5023990000

result:

ok 1 number(s): "5023990000"

Test #9:

score: 0
Accepted
time: 28ms
memory: 149096kb

input:

1
100000 4817
acdcaaccca
cdbbbbabba
bcbccbcdbd
cdabaddaad
ddbdbabcac
dadadbbcba
aabcbbcabc
cdcadbbbda
dabacdbabd
dcbadbdabd
cdcbacbada
cadbbadbac
dbadbccdcd
babaddcdca
aaaddacccc
dabdcdadbb
abbbadbdaa
bcdbbacdaa
bcbcbddadb
bddaccbaba
baddadaaac
adbadbdaaa
cacbbbcdbc
abccdcacdb
abaacddbbc
acbbbcbcdc
...

output:

618356

result:

ok 1 number(s): "618356"

Test #10:

score: 0
Accepted
time: 149ms
memory: 147712kb

input:

10
3901 4952
srsofqyvrt
tazndzviuq
jcomfoxkiw
huzfqiecss
hhdtqpaohy
qcrokphbtf
xzkxssibix
hokmdpzydu
jreaeulsjt
vmxdsazajq
jawyofqbck
cwmzupygdm
rgsahqyxqt
kckgorfgvi
kcawvezmyb
mpcyhdifgn
xiwtmwttmp
mzknfqoifl
vbpvvdnqpy
anpdgjnbew
scekjovqpr
lzhfocjvld
oswjfhjdby
pmdbjddkpv
yjbnjcdtmc
gmpjgtpksa
r...

output:

904
208656
4560
30873
28272
5377
1326
93956
93720
282610

result:

ok 10 numbers