摘要: 经典的Joseph问题吧,说有n个要被处决的人(编号0~(n-1)),从0开始报数,报到(m-1)的会被杀掉,剩下的人继续从0开始报数,如此下去最后剩的一个人会存活下来: 很明显剩下未处死的人编号为0我们来研究一下相邻两次的操作:x:0 1 2 3 4 5 6 …………m-2 m-1 m m+1m+2.....n-1 (有n人)y:n-20 1 2 …… (有n-1人)对比这两次操作发现 同一人的序号 之间的关系:x=(y+m)%n因此可以从后往前递推: 存活人序号:(最后一次):t=0 (倒数第二次):t=(t+m)%2 (有2人) (倒数第三次):t=(t+m)%3 (有3... 阅读全文
posted @ 2012-02-17 16:06 wuzhibin 阅读(286) 评论(0) 推荐(0)
摘要: /*This Code is Submitted by billforum for Problem 1952 at 2012-02-17 13:29:48*/#include <cstdio>#include <iostream>#include <stdlib.h>#include <memory.h>#include <math.h>#include <string.h>using namespace std;const int N=1000005;int next[N];int main(){ string str; 阅读全文
posted @ 2012-02-17 13:32 wuzhibin 阅读(118) 评论(0) 推荐(0)
摘要: KMP 匹配算法是由 "Knuth Morris Pratt" 提出的一种快速的模式匹配算法。hint:不为自身的最大首尾重复子串长度1.待解决的问题:假设P为给定的子串,T是待查找的字符串,要求从T中找出与P相同的所有子串,这称为模式匹配问题。 (可以给出子串在T中的位置) (下文中提到的P和T分别为子串和目标串)让我们先来看个例题:T: t0 t1 t2 t3 .... tm-1 ... tn-1P: p0 p1 p2 p3 .....pm-1从T的最左边开始比较,使得 TK = PK,则匹配成功。2.解决模式匹配问题的方案:A:朴素的模式匹配算法(思路简单,但不够简便 阅读全文
posted @ 2012-02-16 19:08 wuzhibin 阅读(215) 评论(0) 推荐(0)
摘要: Description开学了,同学们都回到了学校,happy457还是改不了晚上吃零食的习惯,为了省点钱,于是在周末他就带上了一定数量的钱来到了沃尔玛超市来选购他想要的东西,超市在那个周末很奇怪,对于某些商品他只卖给同一个人1件,而某些商品他最多只卖给同一个人两件,在没有其它的卖法了,现在happy457给每个商品按照自己的喜好给他们都标记上了一个价值,他想在自己的钱有限的范围内购买的商品的价值最大,请你帮他计算一下他能得到的最大价值。Input有多组输入数据。首先输入m和n,表示他带有m元钱(1<=m<=1000),接着是n(1<=n<=500)行,每行有三个正整数a 阅读全文
posted @ 2012-02-16 18:26 wuzhibin 阅读(238) 评论(0) 推荐(0)
摘要: /*This Code is Submitted by billforum for Problem 1460 at 2012-02-15 17:20:22*/#include <iostream>#include <stdio.h>#include <map>#include <cmath>#include <algorithm>using namespace std;const int N=205;const int Max=20000;int n,ns;int a[N],d[N][N],total;bool f[N];int mi 阅读全文
posted @ 2012-02-15 17:22 wuzhibin 阅读(208) 评论(0) 推荐(0)
摘要: /*This Code is Submitted by billforum for Problem 1751 at 2012-02-15 12:44:59*/#include <iostream>#include <stdio.h>#include <map>#include <cmath>#include <algorithm>using namespace std;const int N=600;const double Max=20000;int n;double a[N],d[N][N],total;bool f[N];voi 阅读全文
posted @ 2012-02-15 12:48 wuzhibin 阅读(151) 评论(0) 推荐(0)
摘要: #include <iostream>#include <stdio.h>#include <cmath>#include <algorithm>using namespace std;const int N=105;const int Max=20000;int n;double a[N],d[N][N],total;bool f[N];struct point{ double x,y;}p[N];double dist(point p1,point p2){ return(sqrt((double)(p1.x-p2.x)*(p1.x-p2.x 阅读全文
posted @ 2012-02-15 11:33 wuzhibin 阅读(143) 评论(0) 推荐(0)
摘要: #include <iostream>#include <stdio.h>#include <cstring>using namespace std;const int N=2005;const int Max=100;int f[N],a[N],d[N][N];int n,total;string str[N];int diff(string str1,string str2){ int num=0; for(int i=0;i<str1.size();i++) if(str1[i]!=str2[i]) num++; return num;}void 阅读全文
posted @ 2012-02-15 10:00 wuzhibin 阅读(165) 评论(0) 推荐(0)
摘要: #include <iostream>#include <cmath>#include <algorithm>using namespace std;const int N=510;const int Max=20000;int n;double a[N],d[N][N];bool f[N];struct point{ int x,y;}p[N];bool cmp(double x,double y){ return (x>y);}double dist(point p1,point p2){ return(sqrt((double)(p1.x-p2. 阅读全文
posted @ 2012-02-14 17:41 wuzhibin 阅读(188) 评论(0) 推荐(0)
摘要: /*This Code is Submitted by billforum for Problem 1653 at 2012-02-14 13:38:42*/#include <iostream>using namespace std;const int N=1005;const int Max=1000002;int n,v[N],p[N][N];bool f[N];int min(int x,int y){ return(x<y?x:y);}void init(){ for(int i=1;i<=n;i++) { f[i]=0; v[i]=0;... 阅读全文
posted @ 2012-02-14 13:41 wuzhibin 阅读(129) 评论(0) 推荐(0)