动态规划 | 北邮OJ | 94. 最小距离查询
https://vpn.bupt.edu.cn/http/10.105.242.80/problem/p/94/
用set大法写了一堆自己都看不懂的代码, 一提交居然AC了, 一发入魂
堪比DP的set大法
#include <bits/stdc++.h>
#define FF(a,b) for(int a=0;a<b;a++)
#define F(a,b) for(int a=1;a<=b;a++)
#define LEN 510000
#define INF 1000000
#define bug(x) cout<<#x<<"="<<x<<endl;using namespace std;char buf[100010];int main()
{// freopen("./in","r",stdin);int N;scanf("%d",&N);while(N--){scanf("%s",buf);int n;scanf("%d",&n);int len=strlen(buf);set<int> pos[26]; //记录某一字母所在的所有位置FF(i,len){pos[buf[i]-'a'].insert(i);}while(n--){char op[20];char data[20];scanf("%s%s",op,data);if(strcmp(op,"INSERT")==0){buf[len]=data[0];pos[buf[len]-'a'].insert(len);//维护len++;buf[len]=0;}else{int p;int ans;sscanf(data,"%d",&p);char c=buf[p];set<int>& tpos=pos[c-'a'];set<int>::iterator it=tpos.find(p);if(it==tpos.end()){ //找不到ans=-1;}else if(tpos.size()==1){ //只记录了找到的一个ans=-1;}else{it++;ans=INF;if(it!=tpos.end()){ans=*it-p;}it--;//复原if(it!=tpos.begin()){it--;if( (p-*it)<ans){ans=(p-*it);}}}printf("%d\n",ans);}}}return 0;
}
动态规划
其实DP也简单.
对于录入的数据, 从左到有做如下状态转移:
- 用pre[k] 记录 字母k最近(最靠右)出现的下标. 未出现用0代替
- 对字符串从左到右扫描, 当前下标为i , 字符为k, 做判断 :
- 如果pre[k]首次出现
pre[k] = i; //仅仅做更新
- 如果pre[k]不是首次出现
j=pre[k] //记录上次位置
pre[k] = i; //做更新
f[i]=i-j; //因为i是最近(右)出现的, 必然距离左边的那个字母最近
f[j]=min(f[j],f[i]) //而左边的, 需要状态转移
欧阳巨佬代码:
// #include <bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<deque>
#include<cmath>
#include<set>
#include<cstring>
#include<string.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
//----------------
#define int long long
#define double long double
//----------------
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
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 << 1) + (x << 3) + (ch ^ 48);ch = getchar();}return x * f;
}
#define sdf(x) x=read()
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define FFor(i, a, b) for (int i = (a); i >= (b); i--)
#define me(a, b) memset(a, b, sizeof(a))
#define addmod(x, y) (x) = ((x)+ (y)) % mod
#define mulmod(x, y) (x) = (((x) % mod) * ((y) % mod)) % mod
#define chkmin(x, y) (x) = (x) <= (y) ? (x) : (y)
#define chkmax(x, y) (x) = (x) >= (y) ? (x) : (y)
#define mid ((l + ((r - l)/2)))
#define Edge struct edge{int to, nx, w;}e[N << 1];int fi[N];int ce = 1;
#define star_link inline void add(int u, int v, int w){e[++ce] = (edge){v, fi[u], w};fi[u] = ce;}
#define Qmul inline ll qmul(ll x,ll y){ll ans=1;for(;y;y>>=1,mulmod(x,x))if(y&1)mulmod(ans,x);return ans;}
#define Go(u) for (int i = fi[u]; i; i = e[i].nx)
#define random(a, b) ((a) + rand() % ((b) - (a) + 1))
#define cnm cout<<"d"
#define bg1(x) cout<<(#x)<<":"<<(x)<<" "<<endl
#define bg2(x,y) cout<<(#x)<<":"<<(x)<<" "<<(#y)<<":"<<(y)<<" "<<endl
#define bg3(x,y,z) cout<<(#x)<<":"<<(x)<<" "<<(#y)<<":"<<(y)<<" "<<(#z)<<":"<<(z)<<" "<<endl
#define bg4(x,y,z,w) cout<<(#x)<<":"<<(x)<<" "<<(#y)<<":"<<(y)<<" "<<(#z)<<":"<<(z)<<" "<<(#w)<<":"<<(w)<<" "<<endl
#define bg5(x,y,z,w,k) cout<<(#x)<<":"<<(x)<<" "<<(#y)<<":"<<(y)<<" "<<(#z)<<":"<<(z)<<" "<<(#w)<<":"<<(w)<<" "<<(#k)<<":"<<(k)<<" "<<endl
int die = 0;
#define Die die++;if(die>100000){cout<<"dead!!!";exit(0);}
#define lson p << 1, l, mid
#define rson p << 1 | 1, mid + 1, r
#define root 1, 1, n
const double eps = 1e-9;
// srand((unsigned)time(NULL));
ll mod = 1e9 + 7;
const int N = 1e6 + 5;int T;
char s[N], ss[N];
int q;
char c;
int n;
int f[N];
int pre[N];void wk(int i) {f[i] = inf;int k=s[i] - 'a';int j = pre[k];if (!pre[k])pre[k] = i;else {f[i] = i - j;pre[k] = i;f[j]=min(f[j],i-j);}
}void wk2(int i) { //这个是我阅读巨佬代码后做的优化, 减少代码量, 逻辑与wk等效f[i] = inf;int k=s[i] - 'a';int j = pre[k];if(pre[k]) {f[i] = i - j;f[j]=min(f[j],i-j);}pre[k] = i;
}signed main()
{freopen("in","r",stdin);sdf(T);while (T--) {scanf("%s", s + 1);n = strlen(s + 1);sdf(q);For(i, 0, 25)pre[i] = 0;For(i, 1, n) {wk(i);}while (q--) {scanf("%s", ss + 1);if (ss[1] == 'I') {scanf(" %c", &c);s[++n] = c;wk(n);} else {int x;sdf(x);x++;if (f[x] == inf)printf("-1\n");else printf("%lld\n", f[x]);}}}
}
动态规划 | 北邮OJ | 94. 最小距离查询相关推荐
- 北邮oj题库刷题计划(更新ing)
北邮oj题库刷题计划(更新ing) 83. A + B Problem 84 Single Number 85. Three Points On A Line 120 日期 121 最值问题 122 ...
- 北邮OJ 141 虚数
北邮OJ 虚数 #include <bits/stdc++.h> using namespace std; typedef struct fushu{int x; //实部 int y; ...
- 校外如何登陆北邮oj
由于北邮oj不对外开放,准备复试的我需要进入北邮oj:其他小朋友也有这个想法.故写此博客. 先找北邮土著借北邮账号,包括一些认识的同学,直系学长学姐,他们都很乐意帮助的~ 下面是校外进北邮校内网的方法 ...
- 北邮OJ 2016 网预-Square Coins
时间限制 1000 ms 内存限制 65536 KB 题目描述 Artoria, also known as Saber-chan, was born into a time of chaos and ...
- 北邮OJ 2016网预 - Saber's Conjecture
时间限制 1000 ms 内存限制 65536 KB 题目描述 In a parallel universe, young Saber-chan does not have a humongous a ...
- 北邮OJ 1027. 16校赛-Archer in Archery
时间限制 1000 ms 内存限制 65536 KB 题目描述 Archer(Emiya), also known as the red A, is famous for his talented s ...
- 北邮OJ 1022. 16校赛-Saber's Board
时间限制 5000 ms 内存限制 65536 KB 题目描述 In a parallel universe, Saber has won the champion of every kind of ...
- 北邮OJ 1021. 16校赛-Stone Game
时间限制 4000 ms 内存限制 65536 KB 题目描述 Alice and Bob are old friends in game theory. This afternoon they me ...
- 北邮OJ 1010. 16校赛-Binary Strings
时间限制 5000 ms 内存限制 65536 KB 题目描述 One day, the teacher asked Weishen to judge whether a binary string ...
最新文章
- python_异常处理
- Shell(12)——awk(2)
- 《MFC dialog中加入OpenGL窗体》
- Unity性能优化的N种武器
- CAN笔记(2) CAN特点
- 从新手到高手 c++全方位学习_股票新手怎样快速入门?关于散户学习炒股的几点建议...
- render_notebook()结果没有图_数与图(20)——机器学习.下
- ASP.NET- 执行SQL超时的解决方案
- typecho 全站ajax,Typecho全站启用HTTPS教程
- 小、巧、快消息队列组件beanstalkd
- JSF使用HTML5的custom attribute
- matlab fig图片读取,从Matlab .fig文件中读取数据,并重新绘图
- SQL Server 由于一个或多个对象访问此列,ALTER TABLE DROP COLUMN xxx 失败问题解决
- 服务器提示临时文件已满,为什么显示“临时文件夹已满或不能访问”
- SIGMOD论文阅读笔记
- 为什么百度蜘蛛不对网站进行抓取?
- LLumar龙膜官宣品牌代言人——虚拟偶像「昤珑」
- 已知信码序列为1011_某一个数据通信系统采用CRC校验方式,其中:生成多项式G(X)=...
- 使用Bootstrap制作网页主界面、增加界面
- 百度wenku的下载