LeetCode 521. Longest Uncommon Subsequence I

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn’t exist, return -1.

Example 1:
Input: “aba”, “cdc”
Output: 3
Explanation: The longest uncommon subsequence is “aba” (or “cdc”),
because “aba” is a subsequence of “aba”,
but not a subsequence of any other strings in the group of two strings.
Note:

Both strings’ lengths will not exceed 100.
Only letters from a ~ z will appear in input strings.

题目大意:给定两个字符串,找这两个字符串中最长非公共子序列。 最长的非公共子序列是指这些字符串之一的最长的子序列,并且这个子序列不是其他字符串的任何子序列。

分析:如果a和b相等,那么a的任意一个子串都是b的子串,反之同理,所以a和b相等时返回-1;如果a和b不相等,返回a和b长度中较长的数字,因为只要取a和b中长度较长的那个字符串,必然是另一方的最长非公共子串~

 

LeetCode 507. Perfect Number

We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.

Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
Example:
Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14
Note: The input number n will not exceed 100,000,000. (1e8)

题目大意:完美数字是指它的所有可以整除的正数中除了它本身,其他数字之和等于这个数字的数。给一个正整数n,写一个函数,当它是一个完美数字的时候返回true否则false。

分析:从2~sqrt(num),累加所有i和num/i【因为如果从1~num一个个试是否可以整除的话会超时,而且也没必要,因为知道了除数a必然就知道了num/a这个数字也是它的除数】因为最后还有一个1没有加,所以sum一开始为1,然后返回num == sum,注意如果num本身为1,则要return false,因为1的唯一一个除数1是它本身不能累加,所以1不满足条件~

 

LeetCode 443. String Compression

Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.

Follow up:
Could you solve it using only O(1) extra space?

Example 1:
Input:
[“a”,”a”,”b”,”b”,”c”,”c”,”c”]

Output:
Return 6, and the first 6 characters of the input array should be: [“a”,”2″,”b”,”2″,”c”,”3″]

Explanation:
“aa” is replaced by “a2”. “bb” is replaced by “b2”. “ccc” is replaced by “c3”.
Example 2:
Input:
[“a”]

Output:
Return 1, and the first 1 characters of the input array should be: [“a”]

Explanation:
Nothing is replaced.
Example 3:
Input:
[“a”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”]

Output:
Return 4, and the first 4 characters of the input array should be: [“a”,”b”,”1″,”2″].

Explanation:
Since the character “a” does not repeat, it is not compressed. “bbbbbbbbbbbb” is replaced by “b12”.
Notice each digit has its own entry in the array.
Note:
All characters have an ASCII value in [35, 126].
1 <= len(chars) <= 1000.

分析:指针i指向修改内容的位置,指针j遍历整个数组chars,当下一个字符与当前字符不相同时,直接将该字符赋值到i处,然后i++,j++;否则若相同,k指向j所在位置,j继续向前出发遍历所有与k处相同的字符,则相同的个数为j-k,将j-k转化为字符串s,将s的每一个字符都赋值在i所在位置开始的chars中~最后直到j>=n时退出循环,此时i的值即为in-place后新数组中的个数~

 

【note】common Git commands(git常用命令)

Git is a tool to save versions of your code.

git init # initialize, set up all the tools Git needs to begin tracking changes made to the project
git status # check the status of those changes
git add <filename> # add a file to the staging area
git add <filename1> <filename2> <filename3> … # commit them to a repository in a single commit
git diff <filename> # check the differences between the working directory and the staging area
# press q on the keyboard to restore the terminal
git commit -m “abcdefg” # A commit permanently stores changes from the staging area inside the repository
git log # Commits are stored chronologically in the repository and can be viewed with ‘git log’
# SHA: A 40-character code, uniquely identifies the commit
git remote add origin https://github.com/liuchuo/123456.git # remote repository url
git push -u origin master # set upstream for git pull on master branch
git push # push the commit to remote repository
git show HEAD # the most recently made commit is the HEAD commit.
# the output of this command will display everything the git log command displays for the HEAD commit, plus all the file changes that were commited.
git checkout HEAD <filename> # restore the file in your working directory to look exactly as it did when you last made a commit
git checkout — <filename> # discard the change in working directory
git reset HEAD <filename> # it does not discard file changes from the working directory, it just removes them from the staging area
git reset <commit_SHA> # rewind to the part before you made the wrong turn
# commit_SHA works by using the first 7 characters of the SHA of a previous commit
# head goes to a previously made commit of your choice, but not discard file changes form the working directory
git branch # check what branch you are currently on (* is showing you what branch you’re on)
git branch <new_branch_name> # create a new branch
# new branch name can’t contain whitesapces
git checkout <branch_name> # switch to the new branch
git merge <branch_name> # merge branch_name to master, firstly switch over to the master branch
# if merge conflict, fix conflicts -> add -> commit, then git merge
git branch -d <branch_name> # delete branch
git clone <remote_location> <clone_name> # get your own replica of the remote repository
# remote_location tells Git where to go to find the remote. This could be a web address, or a filepath
git remote -v # see a list of a Git project’s remotes
git fetch # see if changes have been made to the remote, this command will not merge changes from the remote into your local repository
git merge origin/master # integrate origin/master into your local master branch
git push origin <branch_name> # push a local branch to the origin remote

 

【2017年读书报告+书籍推荐】

0. 2017年读书报告

 

以下推荐书单仅代表个人意见,选自截止至2017-12-31我所读过的所有书籍。

1.推荐书单零(算法类书籍)

《C++ Primer 中文版(第 5 版)》– [美] Stanley B. Lippman / [美] Josée Lajoie / [美] Barbara E. Moo / 王刚 / 杨巨峰 / 电子工业出版社

《数据结构、算法与应用(原书第2版) : C++语言描述》– Sartaj Sahni / 王立柱 / 刘志红 / 机械工业出版社

《算法(第4版)》 塞奇威克 (Robert Sedgewick) / 韦恩 (Kevin Wayne) / 谢路云 / 人民邮电出版社

《算法导论(原书第2版)》– [美] Thomas H.Cormen / Charles E.Leiserson / Ronald L.Rivest / Clifford Stein / 潘金贵 等 / 机械工业出版社

《算法竞赛入门经典(第2版)》– 刘汝佳 / 清华大学出版社

《大学程序设计课程与竞赛训练教材 : 算法设计编程实验》– 吴永辉 / 王建德 / 机械工业出版社


2.推荐书单一(计算机类书籍)

《第一行代码:Android(第2版) : Android》– 郭霖 / 人民邮电出版社

《程序员健康指南》– Joe Kutner / 陈少芸 / 人民邮电出版社

《代码整洁之道》 – [美]Robert C. Martin / 韩磊 / 人民邮电出版社

《代码整洁之道:程序员的职业素养》 – 罗伯特·C.马丁 (Robert C.Martin) / 余晟、章显洲 / 人民邮电出版社

《通灵芯片 : 计算机运作的简单原理》– Daniel Hillis / 崔良沂 / 上海世纪出版集团

《软技能 : 代码之外的生存指南》– John Sonmez / 王小刚 / 人民邮电出版社

《正则表达式必知必会》– Ben Forta / 杨涛、王建桥、杨晓云 / 人民邮电出版社

《C++ Primer Plus : 中文版(第六版)》– Stephen Prata / 张海龙、袁国忠 / 人民邮电出版社

3.推荐书单二(科普类书籍)

《未来简史》– [以色列] 尤瓦尔·赫拉利 / 林俊宏 / 中信出版社

《枪炮、病菌与钢铁 : 人类社会的命运》– [美] 贾雷德·戴蒙德 / 谢延光 / 上海世纪出版集团

《硅谷之谜 : 《浪潮之巅》续集》– 吴军 / 人民邮电出版社

《最强大脑 : 为什么人类比其他物种更聪明》– [巴西]苏珊娜•埃尔库拉诺-乌泽尔 / 缪文

《浪潮之巅(第2版)(上下册)》– 吴军 / 人民邮电出版社

《历史深处的忧虑 : 近距离看美国之一》– 林达 / 生活·读书·新知三联书店

《你一定爱读的极简欧洲史 : 为什么欧洲对现代文明的影响这么深》– 约翰·赫斯特(John Hirst) / 席玉苹 / 广西师范大学出版社

《自私的基因》– (英)里查德.道金斯 / 卢允中 / 吉林人民出版社

《上帝掷骰子吗? : 量子物理史话》– 曹天元 / 辽宁教育出版社

《消失的微生物 : 滥用抗生素引发的健康危机》– [美]马丁•布莱泽 / 傅贺、严青(校) / 湖南科学技术出版社

《理性选民的神话 : 为何民主制度选择不良政策》– (美)布赖恩·卡普兰 / 刘艳红 / 上海人民出版社

《一平米健身:硬派健身》– 斌卡 / 湖南文艺出版社

《小狗钱钱 : 引导孩子正确认识财富、创造财富的“金钱童话”》– [德] 博多·舍费尔 / 王钟欣、余茜 / 南海出版公司

《大学之路》– 吴军 / 人民邮电出版社

《我们在为什么样的广告买单》– [英]罗伯特·希思 / 任永欣、郑昊沫 / 世界图书出版公司

《趣味生活简史》– 比尔·布莱森 / 严维明 / 接力出版社

4.推荐书单三(文学类书籍)

《如何阅读一本书》– [美] 莫提默·J. 艾德勒、查尔斯·范多伦 / 郝明义、朱衣 / 商务印书馆

《武则天正传》– 林语堂 / 湖南文艺出版社

《西厢记》– 王实甫 / 张燕瑾 校注 / 人民文学出版社

《红与黑》– [法] 司汤达 / 罗新璋 / 天津人民出版社

《目送》– 龙应台 / 生活·读书·新知三联书店

《唐诗宋词里的趣事》– 季风 / 北京大学出版社

《看见》– 柴静 / 广西师范大学出版社

《傅雷家书》– 傅敏 编注 / 天津社会科学院出版社

《有闲阶级论 : 关于制度的经济研究》– [美] 凡勃伦 / 蔡受百 / 商务印书馆

PAT 1139. First Contact (30)-PAT甲级(模拟)

Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a girl B, he would usually not contact her directly in the first place. Instead, he might ask another boy C, one of his close friends, to ask another girl D, who was a friend of both B and C, to send a message to B — quite a long shot, isn’t it? Girls would do analogously.
Here given a network of friendship relations, you are supposed to help a boy or a girl to list all their friends who can possibly help them making the first contact.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (1 < N <= 300) and M, being the total number of people and the number of friendship relations, respectively. Then M lines follow, each gives a pair of friends. Here a person is represented by a 4-digit ID. To tell their genders, we use a negative sign to represent girls.
After the relations, a positive integer K (<= 100) is given, which is the number of queries. Then K lines of queries follow, each gives a pair of lovers, separated by a space. It is assumed that the first one is having a crush on the second one.
Output Specification:
For each query, first print in a line the number of different pairs of friends they can find to help them, then in each line print the IDs of a pair of friends.
If the lovers A and B are of opposite genders, you must first print the friend of A who is of the same gender of A, then the friend of B, who is of the same gender of B. If they are of the same gender, then both friends must be in the same gender as theirs. It is guaranteed that each person has only one gender.
The friends must be printed in non-decreasing order of the first IDs, and for the same first ones, in increasing order of the seconds ones.
Sample Input:
10 18
-2001 1001
-2002 -2001
1004 1001
-2004 -2001
-2003 1005
1005 -2001
1001 -2003
1002 1001
1002 -2004
-2004 1001
1003 -2002
-2003 1003
1004 -2002
-2001 -2003
1001 1003
1003 -2001
1002 -2001
-2002 -2003
5
1001 -2001
-2003 1001
1005 -2001
-2002 -2004
1111 -2003
Sample Output:
4
1002 2004
1003 2002
1003 2003
1004 2002
4
2001 1002
2001 1003
2002 1003
2002 1004
0
1
2003 2001
0
分析:1.用数组arr标记两个人是否是朋友(邻接矩阵表示),用v标记所有人的同性朋友(邻接表表示)
2.对于一对想要在一起的A和B,他们需要先找A的所有同性朋友C,再找B的所有同性朋友D,当C和D两人是朋友的时候则可以输出C和D
3.A在寻找同性朋友时,需要避免找到他想要的伴侣B,所以当当前朋友就是B或者B的同性朋友就是A时舍弃该结果
4.输出时候要以4位数的方式输出,所以要%04d
5.如果用int接收一对朋友,-0000和0000对于int来说都是0,将无法得知这个人的性别,也就会影响他找他的同性朋友的那个步骤,所以考虑用字符串接收,因为题目中已经表示会以符号位加四位的方式给出输入,所以只要判断字符串是否大小相等,如果大小相等说明这两个人是同性
6.结果数组因为必须按照第一个人的编号从小到大排序(当第一个相等时按照第二个人编号的从小到大排序),所以要用sort对ans数组排序后再输出结果

Update: 新PAT系统中原代码导致了一个测试点内存超限,使用unordered_map<int, bool> arr 替代二维数组可避免内存超限(2018-05-28更新)