2017年2月

碰到一个题目

题目描述

玛雅人有一种密码,如果字符串中出现连续的2012四个数字就能解开密码。给一个长度为N的字符串,(2=<N<=13)该字符串中只含有0,1,2三种数字,问这个字符串要移位几次才能解开密码,每次只能移动相邻的两个数字。例如02120经过一次移位,可以得到20120,01220,02210,02102,其中20120符合要求,因此输出为1.如果无论移位多少次都解不开密码,输出-1。

输入

第一行输入N,第二行输入N个数字,只包含0,1,2

输出

样例输入

5
02120

样例输出

1

虽然知道是bfs,但实现起来总觉得哪里不对,参考大佬们的博客后解决了几个难点:

  • 存储类型 虽然题目说是输入n个数,但是使用字符串存储会使后续的处理更方便,题目出处九度OJ_1482输入写的也是长度为n的字符串

- 阅读剩余部分 -

转载自ICS161,学英语,自娱自乐

Traversal of graphs and digraphs

To traverse(遍历) means to visit the vertices in some systematic order. You should be familiar with various traversal methods for trees:

preorder: visit each node before its children.
postorder: visit each node after its children.
inorder (for binary trees only): visit left subtree, node, right subtree.

We also saw another kind of traversal, topological ordering(拓扑排序), when I talked about shortest paths.

Today, we'll see two other traversals: breadth first search (BFS) and depth first search (DFS). Both of these construct spanning trees with certain properties useful in other graph algorithms. We'll start by describing them in undirected graphs, but they are both also very useful for directed graphs.



- 阅读剩余部分 -

这两天看了廖雪峰老师的 Git 教程,受益匪浅。

以下为常用基本命令总结:

  • 创建本地版本库:git init
  • 添加文件到库:git add <file>git commit -m "messages"
  • 查看工作区状态:git status
  • 查看修改内容:git diff
  • 查看提交历史:git log
  • 返回历史版本:git reset --hard commit_id
  • 查看返回历史:git relog
  • 丢弃工作区修改:git check -- file
  • 丢弃暂存区修改:git reset HEAD file
  • 删除一个文件:git rm
  • 关联一个远程库:git remote add origin git@server:path/repo.git
  • 关联后第一次推送:git push -u origin master
  • 之后进行推送:git push origin master
  • 克隆远程库:git clone ssh://user@domain.com/repo.git
  • 查看分支:git branch
  • 创建分支:git branch <name>
  • 切换分支:git checkout <name>
  • 创建+切换分支:git checkout -b <name>
  • 合并某分支到当前分支:git merge <name>
  • 删除分支:git branch -d <name>
  • 新建标签:git tag <name> 默认为HEAD
  • 指定标签信息:git tag -a <tagname> -m "balabala……"
  • 查看所有标签:git tag
  • 推送全部未推送过的本地标签:git push origin --tags
  • 删除本地标签:git tag -d <tagname>
  • 删除远程标签:git push origin :refs/tags/<tagname>

什么是 Gist

Gist 是 github 的一个保存代码片的网站,大陆需要翻墙才能访问。

关于 Gist 的管理,推荐一款非常方便的工具:Lepton

在博客中引用 Gist 代码块

复制 js 代码:

如上图的

<scriptsrc="https://gist.github.com/heart4lor/0dda9bea781c5501c3bb50e6f5ab7756.js"></script>

粘贴到Markdown文档中想要的位置即可,像 hexo typecho 这样的博客系统会直接输出如下的代码块:

- 阅读剩余部分 -