EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

08 2012 档案

摘要:Longest Ordered SubsequenceTime Limit:2000MSMemory Limit:65536KTotal Submissions:24010Accepted:10407DescriptionA numeric sequence ofaiis ordered ifa1<a2< ... <aN. Let the subsequence of the given numeric sequence (a1,a2, ...,aN) be any sequence (ai1,ai2, ...,aiK), where 1 <=i1<i2< 阅读全文
posted @ 2012-08-19 22:49 Eric-Yang 阅读(214) 评论(0) 推荐(0)

摘要:Eating TogetherTime Limit:1000MSMemory Limit:65536KTotal Submissions:4548Accepted:2220DescriptionThe cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently numbered 1, 2, and 3) that insist upon dining together. The trouble starts when they 阅读全文
posted @ 2012-08-19 22:48 Eric-Yang 阅读(273) 评论(0) 推荐(0)

摘要:NetworkTime Limit:1000MSMemory Limit:10000KTotal Submissions:6906Accepted:3226DescriptionA Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional 阅读全文
posted @ 2012-08-19 22:47 Eric-Yang 阅读(208) 评论(0) 推荐(0)

摘要:Drainage DitchesTime Limit:1000MSMemory Limit:10000KTotal Submissions:41706Accepted:15608DescriptionEvery time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. 阅读全文
posted @ 2012-08-19 22:46 Eric-Yang 阅读(200) 评论(0) 推荐(0)

摘要:Jungle RoadsTime Limit:1000MSMemory Limit:10000KTotal Submissions:15558Accepted:6963DescriptionThe Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the 阅读全文
posted @ 2012-08-19 22:45 Eric-Yang 阅读(174) 评论(0) 推荐(0)

摘要:Blue JeansTime Limit:1000MSMemory Limit:65536KTotal Submissions:8341Accepted:3494DescriptionThe Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated.As an IBM 阅读全文
posted @ 2012-08-19 22:44 Eric-Yang 阅读(185) 评论(0) 推荐(0)

摘要:Color a TreeTime Limit:1000MSMemory Limit:30000KTotal Submissions:6103Accepted:2057DescriptionBob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is singled out, called the "root" of the tree, and there is a unique path from the root t 阅读全文
posted @ 2012-08-19 22:43 Eric-Yang 阅读(225) 评论(0) 推荐(0)

摘要:PainterTime Limit:1000MSMemory Limit:65536KTotal Submissions:2422Accepted:1509DescriptionThe local toy store sells small fingerpainting kits with between three and twelve 50ml bottles of paint, each a different color. The paints are bright and fun to work with, and have the useful property that if y 阅读全文
posted @ 2012-08-19 22:42 Eric-Yang 阅读(272) 评论(0) 推荐(0)

摘要:EntropyTime Limit:1000MSMemory Limit:10000KTotal Submissions:3387Accepted:1335DescriptionAn entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with "wasted" or "extra" information removed. In other words, entropy encoding remov 阅读全文
posted @ 2012-08-19 22:40 Eric-Yang 阅读(358) 评论(0) 推荐(0)

摘要:Wooden SticksTime Limit:1000MSMemory Limit:10000KTotal Submissions:14948Accepted:6133DescriptionThere is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setu 阅读全文
posted @ 2012-08-19 22:39 Eric-Yang 阅读(263) 评论(0) 推荐(0)

摘要:The Separator in GridTime Limit:1000MSMemory Limit:30000KTotal Submissions:2504Accepted:776DescriptionGiven a connected, undirected graph G = (V, E), where V is the vertex set consisting a collection of nodes, and E is the set of edges, each of which connects two nodes from V. A vertex subset S is a 阅读全文
posted @ 2012-08-19 22:38 Eric-Yang 阅读(254) 评论(0) 推荐(0)

摘要:Finding NemoTime Limit:2000MSMemory Limit:30000KTotal Submissions:6289Accepted:1426DescriptionNemo is a naughty boy. One day he went into the deep sea all by himself. Unfortunately, he became lost and couldn't find his way home. Therefore, he sent a signal to his father, Marlin, to ask for help. 阅读全文
posted @ 2012-08-19 22:37 Eric-Yang 阅读(328) 评论(0) 推荐(0)

摘要:UNIMODAL PALINDROMIC DECOMPOSITIONSTime Limit:1000MSMemory Limit:65536KTotal Submissions:4279Accepted:2105DescriptionA sequence of positive integers is Palindromic if it reads the same forward and backward. For example:23 11 15 1 37 37 1 15 11 231 1 2 3 4 7 7 10 7 7 4 3 2 1 1A Palindromic sequence i 阅读全文
posted @ 2012-08-19 22:36 Eric-Yang 阅读(260) 评论(0) 推荐(0)

摘要:To the MaxTime Limit:1000MSMemory Limit:10000KTotal Submissions:32838Accepted:17175DescriptionGiven a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all t 阅读全文
posted @ 2012-08-19 22:34 Eric-Yang 阅读(197) 评论(0) 推荐(0)

摘要:PalindromeTime Limit:3000MSMemory Limit:65536KTotal Submissions:43460Accepted:14815DescriptionA palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of 阅读全文
posted @ 2012-08-19 22:34 Eric-Yang 阅读(252) 评论(0) 推荐(0)

摘要:DefragmentTime Limit:2000MSMemory Limit:10000KTotal Submissions:2938Accepted:993Case Time Limit:1000MSSpecial JudgeDescriptionYou are taking part in the development of a "New Generation" operating system and the NG file system. In this file system all disk space is divided into N clusters 阅读全文
posted @ 2012-08-19 22:32 Eric-Yang 阅读(200) 评论(0) 推荐(0)

摘要:SticksTime Limit:1000MSMemory Limit:10000KTotal Submissions:100068Accepted:22747DescriptionGeorge took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally an 阅读全文
posted @ 2012-08-19 22:32 Eric-Yang 阅读(172) 评论(0) 推荐(0)

摘要:Power StringsTime Limit:3000MSMemory Limit:65536KTotal Submissions:23530Accepted:9880DescriptionGiven two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication 阅读全文
posted @ 2012-08-19 22:30 Eric-Yang 阅读(154) 评论(0) 推荐(0)

摘要:PeriodTime Limit:3000MSMemory Limit:30000KTotal Submissions:9792Accepted:4477DescriptionFor each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i < 阅读全文
posted @ 2012-08-19 22:29 Eric-Yang 阅读(161) 评论(0) 推荐(0)

摘要:Three powersTime Limit:1000MSMemory Limit:30000KTotal Submissions:4094Accepted:1833DescriptionConsider the set of all non-negative integer powers of 3.S = { 1, 3, 9, 27, 81, ... }Consider the sequence of all subsets of S ordered by the value of the sum of their elements. The question is simple: find 阅读全文
posted @ 2012-08-19 22:28 Eric-Yang 阅读(250) 评论(0) 推荐(0)

摘要:Gone FishingTime Limit:2000MSMemory Limit:32768KTotal Submissions:24255Accepted:7082DescriptionJohn is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but 阅读全文
posted @ 2012-08-19 22:27 Eric-Yang 阅读(357) 评论(0) 推荐(0)

摘要:Max SequenceTime Limit:3000MSMemory Limit:65536KTotal Submissions:13533Accepted:5654DescriptionGive you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N).You should output S.InputThe input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is gi 阅读全文
posted @ 2012-08-19 22:23 Eric-Yang 阅读(148) 评论(0) 推荐(0)

摘要:Maximum sumTime Limit:1000MSMemory Limit:65536KTotal Submissions:27486Accepted:8410DescriptionGiven a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:Your task is to calculate d(A).InputThe input consists of T(<=30) test cases. The number of test cases (T) is given in th 阅读全文
posted @ 2012-08-19 22:17 Eric-Yang 阅读(218) 评论(0) 推荐(0)

摘要:Subway tree systemsTime Limit:1000MSMemory Limit:10000KTotal Submissions:5735Accepted:2370DescriptionSome major cities have subway systems in the form of a tree, i.e. between any pair of stations, there is one and only one way of going by subway. Moreover, most of these cities have a unique central 阅读全文
posted @ 2012-08-19 22:16 Eric-Yang 阅读(235) 评论(0) 推荐(0)

摘要:Jury CompromiseTime Limit:1000MSMemory Limit:65536KTotal Submissions:21248Accepted:5424Special JudgeDescriptionIn Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selec 阅读全文
posted @ 2012-08-19 22:13 Eric-Yang 阅读(236) 评论(0) 推荐(0)

摘要:参见编程珠玑chap4 chap9,和编程之美3.3给定一个有序(不降序)数组arr,,求任意一个i使得arr[i]等于t,不存在返回-1不变式:x[low]< =t<=x[high]int biSearch(int *arr, int low, int high, int t){ int mid; while(low<=high) { mid=low+(high-low)/2; if(arr[mid]==t) { return mid; } else if(arr[mid]>t) ... 阅读全文
posted @ 2012-08-17 16:16 Eric-Yang 阅读(206) 评论(0) 推荐(0)

摘要:1 //µÝ¹éºÍ·ÇµÝ¹é±éÀúTree 2 // 1 3 // / \ 4 // 2 3 5 // / \ / \ 6 // 4 5 6 7 7 //Pre-Order: 1 2 4 5 3 6 7 8 //In-Order: 4 2 5 1 6 3 7 9 //Post-Order:4 5 2 6 7 3 1 10 11 #include <iostream> 12 #include <cstdio> 阅读全文
posted @ 2012-08-16 22:12 Eric-Yang 阅读(224) 评论(0) 推荐(0)

摘要:zzhttp://blog.csdn.net/dlengong/article/details/7408019输入括号的数目,输出括号的各种合法匹配样式 如输入 2输出( ())()()据说这是一道某公司的面试题,我们先来分析一下。括号匹配有合法有的不合法 如 (()))( 这样就不是合法的匹配样式。为了避免这种情况的出现,记录当前左括号的个数和右括号的个数,使右括号的个数不大于左括号的个数。主要思想类似于0-1背包问题,当进行到某一步的时候 有两种方法:放'(' 和 放 ')' 1 #include <iostream> 2 #include &l 阅读全文
posted @ 2012-08-16 21:51 Eric-Yang 阅读(261) 评论(0) 推荐(0)

摘要:字符串比较函数,这个函数也比较常用:view plaincopy to clipboardprint?int strcmp(const char* str1,const char* str2){ assert(str1!=NULL&&str2!=NULL); while(*str1&&*str2&&*str1==*str2){ str1++; str2++; } if(*str1==*str2&&*str1==0) return 0;//equal else if(*str1... 阅读全文
posted @ 2012-08-15 23:17 Eric-Yang 阅读(789) 评论(0) 推荐(0)

摘要:Brackets SequenceTime Limit:1000MSMemory Limit:65536KTotal Submissions:19727Accepted:5465Special JudgeDescriptionLet us define a regular brackets sequence in the following way:1. Empty sequence is a regular sequence.2. If S is a regular sequence, then (S) and [S] are both regular sequences.3. If A a 阅读全文
posted @ 2012-08-15 16:04 Eric-Yang 阅读(185) 评论(0) 推荐(0)

摘要:Mondriaan's DreamTime Limit:3000MSMemory Limit:65536KTotal Submissions:7991Accepted:4605DescriptionSquares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, f 阅读全文
posted @ 2012-08-15 16:01 Eric-Yang 阅读(293) 评论(0) 推荐(0)

摘要:1 #include <iostream> 2 3 using namespace std; 4 5 const int TREELEN=6; 6 7 typedef struct NODE 8 { 9 char chVal;10 struct NODE* pLeft;11 struct NODE* pRight;12 }NODE;13 14 void rebuild(char *pPreOrder, char *pInOrder, int treeLen, NODE **pRoot)15 {16 //前序中序序列为空,返回NULL,失败17 i... 阅读全文
posted @ 2012-08-13 22:59 Eric-Yang 阅读(179) 评论(0) 推荐(0)

摘要:zzhttp://blog.csdn.net/vividonly/article/details/6688327编程之美3.9:重建二叉树扩展问题1:如果前序和中序遍历的字母有重复的,那么怎么构造所有可能的解呢?扩展问题2:如何判断给定的前序遍历和中序遍历的结果是合理的?思路:问题1:搜索所有可能的情况,并调用扩展问题2的解决方案,判断此情况是否合理(剪枝操作),如果合法,则构造解问题2:递归判断左右子树是否合理,递归的返回条件是到达叶子节点。代码及测试情况如下: 1 /* 2 * 编程之美重建二叉树,扩展问题1,2 3 * 扩展问题1:如果前序和中序的字母可能是相同的,怎么重构出所... 阅读全文
posted @ 2012-08-12 21:35 Eric-Yang 阅读(387) 评论(0) 推荐(0)

摘要:zzhttp://www.cppblog.com/humanchao/archive/2008/04/17/47357.html有一个单链表,其中可能有一个环,也就是某个节点的next指向的是链表中在它之前的节点,这样在链表的尾部形成一环。问题:1、如何判断一个链表是不是这类链表?2、如果链表为存在环,如何找到环的入口点?解答:一、判断链表是否存在环,办法为:设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针必定相遇。(当然,fast先行头到尾部为NULL,则为无环链表)程序如下 阅读全文
posted @ 2012-08-12 15:07 Eric-Yang 阅读(250) 评论(0) 推荐(0)

摘要:1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 int main() 8 { 9 char strA[10] = "abddd";10 char strB[10] = "aebdd";11 int lenA, lenB;12 lenA=strlen(strA);13 lenB=strlen(strB);14 15 //dp16 int dp[lenA+1][lenB+1];17 阅读全文
posted @ 2012-08-12 00:24 Eric-Yang 阅读(254) 评论(0) 推荐(0)

摘要:两个字符串可以经过修改,增加, 删除一个字符作为一个操作,经过n步操作,两个字符串变为一样的,相似度就为n,求n。 1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 char strA[10] = "abddd"; 7 char strB[10] = "aebdd"; 8 9 int calcDistance(int sa, int ea, int sb, int eb)10 {11 if(sa > ea)12 {13 if(sb 阅读全文
posted @ 2012-08-11 20:36 Eric-Yang 阅读(230) 评论(0) 推荐(0)

摘要:1 #include <iostream> 2 3 using namespace std; 4 5 char c[10][10] = 6 { 7 "", //0 8 "", //1 9 "ABC", //210 "DEF", //311 "GHI", //412 "JKL", //513 "MNO", //614 "PQRS", //715 "TUV", //816 "WXYZ", 阅读全文
posted @ 2012-08-11 20:05 Eric-Yang 阅读(272) 评论(0) 推荐(0)

摘要:zz:http://blog.csdn.net/jcwkyl/article/details/3889802这是《编程之美》的2.20题目,给出一段C#代码,要求不用电脑,理解程序并回答问题。下面是从C#代码中改写成的C++代码: 1 #include <iostream> 2 #include <limits> 3 using namespace std; 4 int main() { 5 int rg[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, 6 20,21,22,23,24,25,26,27,28,.. 阅读全文
posted @ 2012-08-11 16:12 Eric-Yang 阅读(265) 评论(0) 推荐(0)

摘要:有一个无序,元素个数为2n的正整数数组,把这个数组分成两个n的子数组,子数组和最接近?将其转化为0/1背包问题,将容量为n的背包放入元素,元素的体积为1,重量为其大小,如何装使其接近于重量sum/2,sum为2n的和。 1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 int main() 7 { 8 int n=5; 9 int nArray[10] = {1,5,7,8,9,6,3,11,20,17};10 11 //0/1背包12 int sum=0;13 ... 阅读全文
posted @ 2012-08-11 15:33 Eric-Yang 阅读(257) 评论(0) 推荐(0)

摘要:Maximum sumTime Limit:1000MSMemory Limit:65536KTotal Submissions:27256Accepted:8329DescriptionGiven a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:Your task is to calculate d(A).InputThe input consists of T(<=30) test cases. The number of test cases (T) is given in th 阅读全文
posted @ 2012-08-10 10:55 Eric-Yang 阅读(230) 评论(0) 推荐(0)

摘要:长度为N的整数数组,只允许用乘法,不能用除法,计算任意(N-1)个数的组合成绩最大的一组,并写出时间复杂度 1 #include <iostream> 2 #include <cstdio> 3 #include <ctime> 4 #include <cstdlib> 5 6 using namespace std; 7 8 void randomGen(int *array, int size) 9 {10 srand(time(NULL));11 12 for(int i=0; i<size; i++)13 {14 array[i] 阅读全文
posted @ 2012-08-09 14:30 Eric-Yang 阅读(268) 评论(0) 推荐(1)

摘要:任意一个整正数N,求一个最小的正整数M(M>1),使得M*N的十进制表示形式只包含1和0 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 5 using namespace std; 6 7 int main() 8 { 9 long long M,N;10 N=99;11 long long reminder[N];12 int i,j,k;13 int noUpdates=0;14 for(i=0; i<N; i++)15 {16 rem... 阅读全文
posted @ 2012-08-08 17:04 Eric-Yang 阅读(175) 评论(0) 推荐(0)

摘要:[zz]http://hi.baidu.com/piaoshi111/item/1461bff47b96cedb6225d23d扩展问题随着Tango的发展,管理员发现,“超级水王”没有了。统计结果表明,有3个发帖很多的ID,他们的发帖数目都超过了帖子总数目N的1/4。你能从发帖ID列表中快速找出他们的ID吗?参考上面的解法,思路如下:如果每次删除四个不同的ID(不管是否包含发帖数目超过总数1/4的ID),那么,在剩下的ID列表中,原先发帖比例大于1/4的ID所占比例仍然大于1/4。可以通过不断重复这个过程,把ID列表中的ID总数降低(转化为更小的问题),从而得到问题的答案。代码如下: 1 v 阅读全文
posted @ 2012-08-07 18:45 Eric-Yang 阅读(196) 评论(0) 推荐(0)

摘要:O(n+logN): 排序后+统计O(1+lonN):排序后+查找中间元素(大于一半的元素一定出现在有序序列的中点)O(N):不同元素两两相消View Code 1 #include <iostream> 2 3 using namespace std; 4 5 6 int find(int array[], int N) 7 { 8 int candid, ntimes; 9 10 ntimes=0;11 for(int i=0; i<N; i++)12 {13 if(ntimes==0)14 {15 ... 阅读全文
posted @ 2012-08-07 18:39 Eric-Yang 阅读(246) 评论(0) 推荐(0)

摘要:View Code 1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int a, b; 9 int p1, p2;10 int rlt;11 12 while(scanf("%d%d",&a,&b)!=EOF)13 {14 rlt=0;15 while(a!=0 && b!=0)16 {17 p1=a-(a&(a-1)); //p1为第一次出现一的位... 阅读全文
posted @ 2012-08-06 20:25 Eric-Yang 阅读(363) 评论(0) 推荐(0)