QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#64113 | #4666. Delete And Win | zimindaada | RE | 0ms | 0kb | C++14 | 1.9kb | 2022-11-24 09:08:18 | 2022-11-24 09:08:22 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
namespace ztd{
using namespace std;
typedef long long ll;
template<typename T> inline T read(T& t) {
t=0;short f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-f;ch=getchar();}
while (ch>='0'&&ch<='9') t=t*10+ch-'0',ch=getchar();
t*=f; return t;
}
}
using namespace ztd;
const int maxn = 1e5+7;
char s[maxn], t[maxn];
int n, m, buc[26], nxt[maxn][26];
inline int getDis(int x, int c){
int mn = 1919810;
for(int i = 0; i < c; ++i){
// cerr << i << ' ';
mn = min(mn, nxt[x][i]);
}
return mn-x;
}
int ans = 1919810;
signed main(){
#ifndef lkytxdy
freopen("string.in","r",stdin);
freopen("string.out","w",stdout);
#else
#endif
scanf("%s", t+1); n = strlen(t+1);
scanf("%s", s+1); m = strlen(s+1);
ans = n;
for(int i = 1; i <= min(n,m); ++i){
if(t[i] > s[i]) goto failed;
else if(t[i] < s[i]) goto tryhard;
}
if(n < m){
tryhard:puts("0");
return 0;
}else if(n == m){
puts("1");
return 0;
}else if(n > m) ans = n-m+1;
failed:;
// cerr << ans << '\n';
for(int i = 0; i < 26; ++i) buc[i] = 1919810;
for(int i = n; i >= 1; --i){
for(int j = 0; j < 26; ++j){
if((int)(t[i]-'a') == j) buc[j] = i;
nxt[i][j] = buc[j];
}
}
int nowans = 0, nowpos = 1, finishi = 1919810;
for(int i = 1; i <= m; ++i){
ans = min(ans, nowans + getDis(nowpos, s[i]-'a'));
if(nxt[nowpos][s[i]-'a'] != 1919810){
nowans += (nxt[nowpos][s[i]-'a']-nowpos);
nowpos = nxt[nowpos][s[i]-'a']+1;
}else{
nowans += (n-nowpos+1);
ans = min(ans, nowans);
nowpos = maxn+1;
}
if(nowpos <= n) ans = min(ans, nowans+(n-nowpos+1));
if(nowpos > n || nowans >= ans) {
finishi = i;
break;
}
}
if(finishi < m) ans = min(ans, nowans);
if(ans > n) ans = n;
cout << ans << '\n';
return 0;
}
详细
Test #1:
score: 0
Dangerous Syscalls
input:
pqsrpspqz pqrpqz