自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

Matrix54: My Blog

50% Informatics and 50% Imagination

  • 博客(111)
  • 收藏
  • 关注

原创 LeetCode-Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()

2014-03-29 09:53:48 682

原创 LeetCode-Reverse Integer

Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you

2014-03-22 08:20:37 655

原创 LeetCode-Implement strStr()

Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.[Code]class Solution {public: char *strStr(char *haystack

2014-03-22 08:13:03 673

原创 LeetCode-Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a

2014-03-22 07:30:46 746

原创 LeetCode-Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin

2014-03-22 06:27:35 557

原创 LeetCode-Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe

2014-03-22 02:13:21 596

原创 Topcoder SRM 582 DIV2 500

Problem Statement  Magical Girls are girls who have magical powers. They fight against evil to protect the Earth. Cosmic enemies have just attacked the Earth, and the Magical Girls are going

2013-06-18 20:29:07 897

原创 TopCoder SRM 582 DIV2 250

Problem Statement  Magical Girl Iris loves perfect squares. A positive integer n is a perfect square if and only if there is a positive integer b >= 1 such that b*b = n. For example, 1 (=1*1

2013-06-18 15:20:21 1006

原创 TopCoder SRM480 DIV1 Practise

Problem Statement  TopCoder Security Agency (TSA, established today) is going to search for dangerous content in the internet.There are N candidate websites numbered 0 through N-1. Each

2013-06-04 09:51:47 919

原创 pat 1055 The World's Richest

题意其实是求数组中的前k小的数。简单的方法大家都想得到,先排序,再取前k个,第二个不出意料的超时。要想以线性期望时间通过,应该得用随机选择算法来做。暂时先放着,以后再来解决。代码:#include#include#includeconst int MAXN = 100005;struct human{ char name[10]; int age; int net_wort

2013-05-24 21:46:52 1138

原创 pat 1054 The Dominant Color

求数组中出现次数超过一半的数字,排序再取中位数太慢,hash表记录出现次数,空间复杂度太高,从另一个角度来看,出现次数超过一半的数字,其出现次数比其它所有数出现之和还多,每次从数组中除去两个不同的数字,直到最后,剩下的肯定是所求。用cad(候选)表示所求的数,nTime为它出现的次数。依次遍历数组,若a[i]和cad不同则nTime--; 若相同则nTime++;  若nTime为0, 则取下

2013-05-24 16:20:53 1813 3

原创 pat 1051 pop sequence

这题对于自己,意义不小,编程能力还是如此之弱!sigh!  用cur标记入栈序列的最大值,逐个扫描出栈序列x,当x与当前栈顶元素不一致(大于或小于)时就将cur++入栈(即使是x大于栈顶元素也将cur++入栈,它必将超过栈容量)。当x与当前栈顶元素一致时,将栈顶元素出栈即可。若栈内元素超过栈容量,则不合法。AC代码:#include#includeusing namespace s

2013-05-24 10:51:27 978

原创 剑指offer 2 重建二叉树

感觉所有case都过了,但就是WA。到时再看看。题目已经说了第一行n,(1#includeconst int MAXN = 15;int preorder[MAXN];int inorder[MAXN];struct node{ int val; node *left; node *right;};int fg = 1;node * cr

2013-04-07 16:59:31 729

原创 总结

压标 //23:46 16分#includeconst int NUM=100005;int a[NUM];int v[NUM];int main(){ int n,i,val; int index=-1; //freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r"

2013-03-10 10:04:25 767

原创 pat 1045 Favorite Color Stripe

比较简单的dp题。假设dp[s]为s长度的字串的最大值。则状态转移方程为:dp[s]=dp[r]+1  ( seq[s]>seq[r]&& dp[r]+1>dp[s]   )。就判断最后一个字母要不要加入即可。AC代码:#include#define N 200#define M 10000using namespace std;int main(void){ int

2013-03-09 10:16:48 1386

原创 pat 1049 Counting Ones

第4,6个点超时。后来发现居然是编程之美的一道原题,用了书上代码,直接AC。代码: //1049 20:14-20:35#includeconst int NUM=100000000;int find(int x){ int res=0; while(x){ if(x%10==1) res++; x=x/10; } return re

2013-03-07 20:36:32 1215

原创 1048 Find Coins

终于拿下了一题,其实很简单,输入时就将b[a[i]]标记为1, 若发现有b[m-a[i]]为1,取其最小值。AC代码: //1048 19:48-20:02#includeconst int NUM=100005;int a[NUM];int b[NUM];int main(){ int n,i,m; int temp,min=NUM; //freo

2013-03-07 19:58:55 955

原创 pat 1047 Student List for Course

与之前的一题类似,最后一点超时,是O(m*nlogn)。参考网上代码,不要每次都遍历,只遍历一遍就好。对于每一门课,都将其选课学生标记下来,再遍历一遍学生,若有标记的,则输出。代码://1047 19:20-19:38#include#include#include#include#includeusing namespace std;map >m;

2013-03-07 19:32:21 1046

原创 pat 1046 Shortest Distance

题意是求一个环上的两点之间最短距离,纯粹模拟,最后一点超时,过不了。后来经人提示,不用保存各点的值,只要保存从起点到该点的值,即可。代码://1046 18:30 -19:14#includeconst int NUM=200008;int a[NUM];int main(){ int n,i,m,x,y,j; //freopen("C:\\Document

2013-03-07 19:13:56 905

原创 pat 1038 Recover the Smallest Number

此题不能简单对其进行排序,因为32不一定排321前面,后来发现规律:对于最小的序列中的任意两个数 a,b 如果 a在b前面 当且仅当 a在b前面 AC代码://1038 19:12 -19:43#include#include#include#includeusing namespace std;char str[10005][10]; // bool cm

2013-03-07 15:32:21 976

原创 pat 1044 Shopping in Mars(16分)

严重超时。只过了第1,4个点,得12分,以后再说。 后来换了思路,还是只得了16分,只有第2点错误,第4点超时,想不出不过的case。//23:46 16分#includeconst int NUM=100005;int a[NUM];int main(){ int n,i,val; int index=-1; freopen("C:\\Document

2013-03-07 01:16:53 1500 1

原创 pat 1042 Shuffling Machine

洗牌机,每次将牌洗到新的位置,输出洗K次后牌的顺序。简单,水题。在纸上写一下就清楚了,只是输出时要注意,S13,J1等。AC代码://1042 22:18#includeint a[60];int b[60];char c[5]="SHCD";int trans[60];int main(){ int k,i,j,num; //freopen("C:\\Document

2013-03-07 00:06:59 1057

原创 pat 1041 Be Unique

太简单的水题了,直接标记即可。AC代码:#includeint a[100005];int c[100005]={0};int main(){ int n; //freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin); scanf("%d",&n); in

2013-03-06 22:04:59 1115 1

原创 pat 1040 Longest Symmetric String

题意要求最长回文串,遍历一遍,找到回文串最中间的那个字母(BAB中的A或ABBA中的第一个B),再以该字母为中心,往左右拓展,若能相等,则相加。就算样例可以过,也要测试一下其它简单的情况,如AAA, BB。要注意的数据点有:aaa     3   aassaa 6 AC代码://1040 21:20#include#includechar str[1005];

2013-03-06 21:51:23 859

原创 pat 1039 Course List for Student(23分)

简单题,定义一个map >即可。将对应学生的选课进行排序,但最后一点总是超时,换成scanf和printf,还是超。以后再看吧。代码://1039 20:28-21:12#include#include#include#include#includeusing namespace std;map > m;int main(){ int n,i,k

2013-03-06 21:02:45 978

原创 pat 1037 Magic Coupon

简单题,先将两数列从小到大排序,再遍历个数少的数列,使得短数列:[----]               [++++]对    应:|    |               |         |长数列:[---------][++++++++]负数与长数列的头部相乘,正数与长数列的尾部相乘。AC代码://1037 17:57-18:59#include#include

2013-03-06 19:04:57 1095

原创 pat 1036 Boys vs Girls

太简单了,就不说了,将男生组和女生组排序,取女生组最高分和男生组最低分。AC代码://1036 16:16 - 16:44#include#includeusing namespace std;const int MAXN=1005;struct student{ char name[14]; char gender; char sid[14]; i

2013-03-06 16:40:46 732

原创 pat 1035 Password

估计是pat里最水的水题了,只要将对应的字母更换即可。AC代码://1035 15:50 - 16:16#include#includeconst int MAXN=1005;struct account{ char name[14]; char pass[14]; int mod;}a[MAXN];int main(){ int n,i

2013-03-06 16:12:02 868

原创 pat 1034 Head of a Gang

题意是要求总共有几个集合,每个集合权重最大的是哪个,以及每个集合中结点个数。一开始老想用并查集来做,发现实现起来很困难。参考网上代码,发现可以用DFS遍历,数出每个分支的结点个数。其中map的迭代器的使用得注意。还有,若一个gang中同时有多个相同的最大值,取字母序最小的。虽然不加这点,也能过,但为了健壮性,加上吧。 AC代码:#include#include#include

2013-03-06 11:10:34 1863

原创 pat 1033 To Fill or Not to Fill(值得重点回顾)

有点难度的贪心题,贪心策略有点绕,得静下心来分析,同时得增强自己的代码能力。实质是汽车加油问题。贪心策略:假设现在自己处于A站,要考虑的是A站要不要加油,加多少油的问题。找到下一个要加油的站B(距离A站cmax*davg范围内的最便宜的站)。1. A站油价比B价高,现在油箱里还有油,能跑到B站,那就不加油,直接跑去(这里B站跟2,3情况不同,是距离A站currGas*davg范围内的最便宜

2013-03-06 10:10:06 2369 1

原创 pat 1031 Hello World for U

简单摸拟题,找出最大的n1即可。AC代码:简单模拟 //1031 0:10-0:37#include#includeint main(){ int n1,n2; char str[100]; int i,j,k; scanf("%s",str); int len=strlen(str);/* n1=len/3;*/

2013-03-06 09:51:35 1121

原创 pat 1032 Sharing

找两条链表的公共结点,先遍历其中一条,并且一一做访问标记,再遍历第二条,一旦访问过就输出结点值。结点值一开始一直用string来保存,用了map,太麻烦,最后一点超时了。参考网上程序,改用int型来保存,顺利ac。有时需要的是思路的转换。 #includeconst int NUM=100005;int a[NUM];int visit[NUM];int main(){

2013-03-05 20:14:25 1319

原创 pat 1030 Travel Plan

虽然是30分题,其实是简单题,只怪自己不清晰,想到什么dijskra和bellmanford的去了,浪费了不少时间。后来直接用DFS解决。//1030 23:00#includeconst int NUM=505;const int INF=0x7fffffff;int map[NUM][NUM],cost[NUM][NUM];int visit[NUM];in

2013-03-03 01:32:18 668

原创 pat 1029 Median

两个数列求中位数,简单排序取中位数,却超时了3组,以后再看。后仔细看题目发现,两个数列已是排好序了的,改用合并排序,过了。我的程序跟别人的一模一样,却有两点没过。//1029 22:00#include#includeconst int NUM=10000005;long a[NUM],b[NUM];int cmp(const void *a, const void *b)

2013-03-02 22:54:07 648

原创 pat 1028 List Sorting

简单排序题,最后一个点估计是输入超时,以后再改。最后一个改成用scanf和printf后就过了。另外,sort的仿函数返回值要用bool,不要用int,不然会出现意想不到的情况。 //21:20#include#include#include#includeusing namespace std;const int NUM=100005;struct stude

2013-03-02 21:50:49 540

原创 pat 1027 Colors in Mars

简单题 进制转化 //1027 20:40#include#includeusing namespace std;char ra[15]="0123456789ABC";vector a[3];void tenToNum(int order,int x){ int i=0,j; if(x==0) a[order].push_back(0); e

2013-03-02 21:12:33 474

原创 pat 1025 PAT Ranking

简单题,只是一开始用vector rank[NUM]用得不太熟,巩固了一下sort的compare函数写法。得抓紧时间做题了,以后博客先略写,有空再来补充。#include#include#include#includeusing namespace std;struct student{ string id; int score; int loc_id; int

2013-03-02 20:34:08 426

原创 pat 1021 Deepest Root

题目是要判断图是否都连接构成树,求使树高最大的所有的根,实际上求图上两点间最大距离。我的思路是依次取各点进行DFS,记下各点可达的最大深度,再在其中取最大值,若各点的最大深度等于该值,该点为所求。若最大深度不等于n-1,则不是一棵树,再用并查集判断有几部分。可能思路有问题,WA了N久。参考网上程序,发现树的最长路径其实有个很方便的求法。任取一个点x,求出距离x最远的一个点y,然后求出距离y最远的一

2013-02-28 22:59:04 1020

原创 pat 1017 Queueing at Bank(23分)

实质上是一个有截止时间的多任务调度问题,多个窗口,多个顾客,一个等待队列,截止时间为17点,算平均等待时间。思路是进行模拟,以i来模拟时间一秒一秒,每一秒都遍历所有窗口,若有窗口空闲且有顾客在等待,就服务。差一个点没过,不知是何种情况。需要考虑的情况有:1. 全部都在17点以后到,这样结果为0.02.一个窗口服务完后,再遇到顾客,得服务3.若顾客17点前到,但17点前无法得到服务,该顾

2013-02-28 14:42:54 1207 1

原创 pat1016 Phone Bills

题目不难,但输入输出数据多,格式多,需要些许思维和注意。花费了非常多时间,也算有些收获。首先,卡在输入输出上,string类型只能用cin,不能用scanf,printf ,若要用printf,只能用printf("%s",str.c_str())。输入中若带“:”,得用getchar()。在printf时,最后一个%02d少加了个%,一直报越界。要细心。此题发现,pat测试数据爱出在临界条件上,

2013-02-28 10:10:08 1127

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除