1651

终于A了 

看这题容易想到最短路 看到错的很多 还特意注意了好几处

后来发现 必须按给出的顺序出边 想了想 这不就是BFS 

然后就是各种细节 i->i+1ori->j(a[i]==a[j])

 

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<vector>
using namespace std;
#define N 10010
#define INF 0xfffffff
vector<int>ed[N];
vector<int>::iterator it;
int vis[N*],pa[N*],a[N*],o[N*],f[N*];
int n,ff[N*];
struct node
{
int x,num;
};
void bfs(int s,int e)
{
int i,oo,minz = INF;
queue<node>q;
node st,te;
st.x = s;
st.num = ;
pa[s] = s;
q.push(st);
while(!q.empty())
{
te = q.front();
int u = te.x;
q.pop();
if(te.num>minz)
continue;
if(a[u]==e)
{
minz = te.num;
oo = u;
continue;
}
if(ff[u]&&!vis[ff[u]])
{
int v = ff[u];
if(!vis[v])
{
vis[v] = ;
st.x = v;
st.num = te.num;
q.push(st);
pa[v] = pa[u];
}
}
if(!vis[u+]&&u<n)
{
vis[u+] = ;
st.x = u+;
st.num = te.num+;
q.push(st);
pa[u+] = u;
}
}
int x = oo,g=;
o[g++] = a[oo];
while(x!=s)
{
x = pa[x];
o[g++] = a[x];
}
for(i = g- ; i >= ; i--)
if(o[i]!=o[i+])
printf("%d ",o[i]);
puts("");
}
int main()
{
int i;
scanf("%d",&n);
for(i =; i <= n ; i++)
{
scanf("%d",&a[i]);
if(f[a[i]])
{
ff[f[a[i]]] = i;
f[a[i]] = i;
}
else
f[a[i]] = i;
}
bfs(,a[n]);
return ;
}

1651. Shortest Subchain(bfs)的更多相关文章

  1. [Algorithm] BFS vs DFS

    //If you know a solution is not far from the root of the tree: BFS, because it is faster to get clos ...

  2. algorithm@ find the shortest path in a graph using BFS

    Finding Shortest Paths By BFS

  3. [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  4. Codeforces-A. Shortest path of the king(简单bfs记录路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  5. Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)

    Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  6. LeetCode934.shortest bridge【dfs+bfs】

    一.题面 在给定的二维二进制数组 A 中,存在两座岛.(岛是由四面相连的 1 形成的一个最大组.) 现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛. 返回必须翻转的 0 的最小数目.( ...

  7. cf 843 D Dynamic Shortest Path [最短路+bfs]

    题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...

  8. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  9. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

随机推荐

  1. JAVA算术运算符、关系运算符和位运算符

    算术运算符 1.java的算数运算符包括+(加).-(减).*(乘)./(除).%(取余),在运算过程中出现的隐式转换原则和C语言一样:2. 高位数据向低位数据转化要使用强制转化: 关系运算符 1.j ...

  2. OC的@property 和 @synthesize id

    学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...

  3. c语言编程之栈(数组实现)

    用数组实现的顺序栈,完成了出栈入栈功能. #include"stdio.h" typedef int element; #define max 100 typedef struct ...

  4. 三大主流开源硬件对比:Arduino vs BeagleBone vs Raspberry Pi

    个人总结: Arduino就是个AVR单片机,个人觉得更适合玩电子的,社区也很活跃. BeagleBone是ARM Cortex-A8,属于嵌入式,价格高于Pi,但是许多方面拥有超越 Pi 的优 势, ...

  5. iOS$299企业账号In House ipa发布流程

    1.在Mac系统中进入“钥匙串访问”,选择“钥匙串访问”-“证书助理”-“从证书颁发机构请求证书”. 填写前两项,并保存在本地. 2.登录https://developer.apple.com,进入i ...

  6. Ext学习-前后交互模式介绍

    在前后台交互模式的介绍中,实际上就是Store中Proxy相关的内容,比如Ajax提交. 所以详细的文档请参考: Ext学习-基础概念,核心思想介绍   中关于数据模型和MVC结构部分. 作者:sdj ...

  7. ApplicationContext

    参考网址: http://baike.baidu.com/link?url=IPzNiVScxSd6ijhDeCKKEuywPqisDeTfyYSQIPRZqLxy6onkPddfzyvcWQC6_M ...

  8. 01-08-05【Nhibernate (版本3.3.1.4000) 出入江湖】NHibernate二级缓存:第三方MemCache缓存

    一.准备工作 [1]根据操作系统(位数)选择下载相应版本的MemCache, MemCache的下载和安装,参看: http://www.cnblogs.com/easy5weikai/p/37606 ...

  9. 【锋利的JQuery-学习笔记】Tab选项卡的实现

    效果图: 关键点: 1.标签和标签内容都是用<ul><li>实现的,主要是通过Css样式设计成选项卡的模样. 2.用js代码实现点击标签时,标签内容的切换(做法是<div ...

  10. CC150 上面重要的题目总结

    第一章 : 全部重要 (1.6, 1.7 Leetcode上有). 1.5 面A碰到 (string compression) 1.7面Z碰到 (set 0) 1.8面Bigfish碰到 (strin ...