- 博客(60)
- 资源 (4)
- 收藏
- 关注
原创 poj 1915 Knight Moves
#include #include using namespace std;const int MAX = 310;int map[MAX][MAX], row, r, l, sx, sy, ex, ey;bool vis[MAX][MAX];//坐标的变换,即是可以移动的方格 int dir[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}
2012-08-31 09:37:55 656
原创 poj 1321 棋盘问题
#include using namespace std;const int MAX = 10;char map[MAX][MAX];int vis[MAX], n, k, ans, sum;//地图的输入 void init(){ int i, j; for (i = 0; i < n; i++){ getchar();
2012-08-30 09:31:05 687
原创 poj 1154 LETTERS
#include #include using namespace std;const int MAX = 25;char matrix[MAX][MAX];int r, s, ans;bool vis[30];void init(){ int i, j; for (i = 0; i < r; i++){ getchar();
2012-08-29 16:24:33 670
原创 poj 1606 Jugs
#include #include #include using namespace std;const int MAX = 105;int a, b, n, vis[MAX][MAX], step[MAX*MAX], l, r;string opr[7] = {" ", "fill A", "fill B", "empty A", "empty B", "pour B A", "po
2012-08-29 10:36:02 690
原创 poj 3414 Pots
//和poj1606的题目的解法是一样的,不过有点地方需要注意,这题当没解的时候要输出impossible,还有无论是哪个杯的水的容量符合要求就可以输出了! #include #include #include using namespace std;const int MAX = 110;int a, b, c, r, l, vis[MAX][MAX], step[MAX*MAX];
2012-08-29 10:35:12 1258 1
原创 poj 3210 Coins
/*思路:若n为偶数: 1: 若初始状态为偶数正面 + 偶数反面,要想变成全正或全反,翻转的次数必为偶数。 例如: ○○●●●● 则翻转 2,4,6,8……次均可。 2: 若初始状态为奇数正面 + 奇数反面,要想变成全正或全反,翻转的次数必为奇数。 例如: ○○○○○● 则翻转 1,3(先将●翻为○,再将任一个○翻两下),5,7……次
2012-08-27 15:59:12 738
原创 poj 3278 Catch That Cow
#include #include #include using namespace std;const int MAX = 100001;int n, k, path[MAX];bool vis[MAX];void bfs(){ int tmp; queue q; //队列的初始化 while (q.size() != 0)
2012-08-27 15:20:08 498
原创 poj 1979 Red and Black
//这题属于简单的搜索题,可以分别用bfs和dfs来做! #include #include #include #include using namespace std;const int MAX = 25;char map[MAX][MAX];int i, j, w, h; int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
2012-08-26 10:55:23 636
原创 poj 3974 Palindrome
#include #include const int MAX = 1000005;char str[MAX], Manacher_str[2*MAX];int len, tc = 0, p[2*MAX];using namespace std;void change(){ int i; Manacher_str[0] = '@'; Manacher
2012-08-26 08:47:37 834
原创 Manacher算法:求解最长回文字符串,时间复杂度为O(N)
回文串定义:“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。回文子串,顾名思义,即字符串中满足回文性质的子串。经常有一些题目围绕回文子串进行讨论,比如POJ3974最长回文,求最长回文子串的长度。朴素算法是依次以每一个字符为中心向两侧进行扩展,显然这个复杂度是O(N^2)的,关于字符串的题目常用的算法有KMP、后缀数组、AC 自动机,这道题目
2012-08-26 08:44:57 35182 9
原创 poj 3637 Shopaholic
//贪心算法,简单! #include #include #include using namespace std;const int MAX = 20001;int prices[MAX];bool cmp(int a, int b){ return a > b;}int main(){ int i, tc, num, ans; scanf(
2012-08-24 20:52:18 1032
原创 poj 3664 Election Time
#include #include #include using namespace std;struct Info{ int index, fv, sv;}info[50001];bool cmp1(Info a, Info b){ return a.fv > b.fv;}bool cmp2(Info a, Info b){ retur
2012-08-24 18:56:16 870
原创 poj 3619 Speed Reading
#include using namespace std;int main(){ int n, k, s, t, r, i, j, ans, tmp, p; while (cin >> n >> k){ for (i = 0; i < k; i++){ cin >> s >> t >> r;
2012-08-24 16:21:56 840
原创 poj 3750 小孩报数问题
#include #include #include #include using namespace std;int main(){ int num, w, s, i, c; string name, tmp; queue q; queue ans; cin >> num; for (i = 0; i < num; i++){
2012-08-23 22:50:00 1057
原创 poj 3978 Primes
#include using namespace std;const int MAX = 100010;int prime[MAX];int main(){ int i, j, a, b, ans; prime[0] = false, prime[1] = false; for (i = 2; i < MAX; i++) prime[i] =
2012-08-22 18:19:54 725
原创 poj 3979 分数加减法
#include #include #include #include using namespace std;//求最大公约数 int gcd(int n, int m){ int tmp; if (n < m){ tmp = m; m = n; n = tmp; } while (n%m){
2012-08-22 12:54:25 828
原创 poj 3982 序列
#include #include #include using namespace std;string str[1000];int n;string solve(string str1, string str2, string str3){ int i, j, c, nn, len1, len2, len3, num1[100], num2[100], num3[
2012-08-22 09:58:39 630
原创 poj 3126 Prime Path
#include #include #include using namespace std;const int MAX = 100000;//竟然RE了三次,就是因为这里的MAX开小了! int step[MAX];bool prime[MAX];int base[4] = {1, 10, 100, 1000};int main(){ int i, j, tc, nu
2012-08-21 15:48:11 377
原创 poj 3173 Parkside's Triangle
#include using namespace std;const int MAX = 25;int triangle[MAX][MAX];int main(){ int n, s, i, j, k; bool flag = false; cin >> n >> s; for (i = 0; i <= n; i++){ for (j =
2012-08-21 12:05:15 939
原创 poj 3176 Cow Bowling
#include #include using namespace std;const int MAX = 400;short dp[MAX][MAX]; int main(){ int n, i, j; while (cin >> n){ for (i = 1; i <= n; i++){ for (j = 1;
2012-08-20 10:58:54 423
原创 poj 1163 The Triangle
#include #include using namespace std;const int MAX = 110;short dp[MAX][MAX]; int main(){ int n, i, j; while (cin >> n){ for (i = 1; i <= n; i++){ for (j = 1;
2012-08-20 10:49:34 323
原创 poj 1159 Palindrome
//这题的题意主要是要求我们求出给出的字符串和其反过来的字符串的最长公共子序列,//然后再用字符串的长度减去其最长公共子序列的长度就可以得出答案了! #include #include #include using namespace std;short dp[5010][5010]; int main(){ int i, j, len; char str1
2012-08-20 09:52:06 375
原创 poj 1080 Human Gene Functions
/*假设输入的字符串分别为 A,Bf[i][j] = { 从 A[i] 和 B[j] 开始匹配,所能达到的最大值 }假设 A[i] = G,B[j] = C那么现在的情况就是 GxxxxxCxxxxx状态转移为=> f[i + 1][j] + value(A[i], '-')G...-C..=> f[i][j + 1] + value(B[j], '-')-G..C..
2012-08-20 09:05:47 390
原创 最长公共子序列(LCS)的两种求解方法
#include #include using namespace std;const int MAX = 1000;int dp[MAX][MAX];int main(){ char str1[MAX], str2[MAX], LCS[MAX]; int i, j, len1, len2; while (1){ cin.getline(
2012-08-19 09:09:35 958
原创 poj 1836 Alignment
//好不容易才弄明白这题需要求解的答案是什么,之前一直都弄不明白题意!发觉这个博客http://cavenkaka.iteye.com/blog/1542421//分析得不错,就是通过看他的解释,才弄明白题目需要求解的答案! #include #include using namespace std;double height[1010];int lis[1010], lds[101
2012-08-18 23:32:25 343
原创 poj 3671 Dining Cows
//这题我以为是和3670是一模一样的题目,就直接将3670的代码复制过来,点知WA了一次,然后再看看题目//原来这一题只是求最长递增子序列就OK了,呵呵,抵死的WA,谁叫不读题! #include #include #include #include #include using namespace std; const int MAX = 30001; int num
2012-08-18 15:50:21 1130
原创 poj 3670 Eating Together
//最长递增子序列(LIS)和最长递减子序列(LDS)的解法其实是一样的,题目就是要求需要改动的次数最少,所以只要//求出其序列的最长递增子序列和最长递减子序列,通过比较就可以最长的子序列,从而就可以求出改动次数最少答案了! #include #include #include #include #include using namespace std; const int
2012-08-18 11:13:20 654
原创 poj 1631 Bridging signals
//求最长递增子序列,和题目2533、1887、3903等是同一类型的题目!做法差不多! #include #include #include using namespace std;const int MAX = 400010; int single[MAX], tmp[MAX]; int main(){ int tc, i, n, left, right, mi
2012-08-18 10:04:57 333
原创 最长递增子序列(LIS)的三种求解方法
//求最长递增子序列的方法有很多,下面就我接触到的三种方法来写一下实现代码:第一种方法是DP,//第二种是DP+二分查找,第三种是转换为求LCS的方法!而第二种的时间复杂度占优势:O(nlogn)! //第一种方法的DP代码: #include using namespace std;const int MAX = 100000;int dp[MAX];int main(){
2012-08-18 09:36:36 723
原创 poj 1887 Testing the CATCHER
//我都不敢相信,这是World Finals 1994的第二题,就是简单的LIS做法,只不过需要注意的地方确实很多,需要很细心, //而且需要有耐性去读懂题目!这题用二分+DP,0MS过了,数据还是比较弱的了!//求解LIS的两种方法都写了,第一种的效率明显要比第二种的效率要高,第一种时间复杂度为O(n^2),第二种时间复杂度为O(nlgn)! #include #include u
2012-08-17 20:54:20 548
原创 poj 3903 Stock Exchange
//和2533是同一类的题目,都是简单的LIS题目,这题用二分+DP比较好,因为数据比较大!容易超时! #include using namespace std;long num[100001], tmp[100001]; int main(){ int n, i, j, len, left, right, mid; while (cin >> n){
2012-08-17 20:02:09 1096
原创 poj 2533 Longest Ordered Subsequence
//有两种做法:一种是DP,另一种是二分查找! #include using namespace std;int num[1010], dp[1010];int main(){ int n, i, j, ans = 0; cin >> n; for (i = 0; i < n; i++){ cin >> num[i]; } f
2012-08-17 15:21:20 615
原创 poj 2853 Sequence Sum Possibilities
/*其实这个题目就是一个等差数列的问题嘛!连续数,公差为1,可以假设有i个元素的和为给定的num,用for循环依次遍历。然后根据等差数列求和num=i*a1+i*(i-1)*d/2, d即是公差1;可以根据判断a1%i是否为零,判定是否存在i个连续的整数的和为num,而且a1即是该等差数列的首项。如果是的话,则ans++*/#include #include using names
2012-08-16 23:55:53 1061
原创 poj 2140 Herd Sums
#include using namespace std;int main(){ int n, i, j, sum, cnt = 0; cin >> n; //经分析可知,其i的取值不可能大于n/2+1,那样范围就缩小了! for (i = 1; i <= n/2+1; i++){ sum = 0; for (j = i;
2012-08-16 21:24:23 1407
原创 qsort和sort的详解和比较函数的区别
下面先对qsort和sort进行详细的解释,然后再将区别!(需要特别注意的就是qsort和sort的比较函数的写法,很容易出错)First qsort基本快速排序的方法,每次把数组分成两分和中间的一个划分值,而对于有多个重复值的数组来说,基本排序的效率较低。集成在C语言库函数里面的的qsort函数,使用三路划分的方法解决这个问题。所谓三路划分,是指把数组划分成小于划分值,等于划分
2012-08-16 18:17:53 10756 2
原创 poj 1844 Sum
//这题如果仔细一点儿分析,结果就出来了!题意:给出一个数,通过从1到N的连续数的运算而得出这个数//(两个数之间的符号只可以是+或-),求最小的N。 //先通过求从1到N的前N项和,如果是小于这个num数的,肯定满足不了,所以继续相加!如果是可以等于num的情况之下,就得出了结果,可以输出!//如果找不到相加和等于num这个数的,就肯定是大于这个num数的了,就只有通过变换数与数之间的符号
2012-08-16 11:09:12 786
原创 poj 1840 Eqs
//下面的做法为直接链址法 #include #include using namespace std;const int MAX = 18750001;//这个数的由来,由其系数最大值决定:50^4 = 18750000; short hash[MAX*2 + 10];int main(){ short a1, a2, a3, a4, a5, x1, x2, x3, x
2012-08-16 10:05:40 505
原创 哈希表的详解
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。哈希表的做法其实很简单,就是把Key通过一个固定的算法函数既所谓的哈希函数转换成一个整型数字,然后就将该数字对数组长度进行取余,取余结果就当作数组的下标,将va
2012-08-16 10:03:06 7860
原创 poj 1604 Just the Facts
//通过打表就OK,求阶乘的末尾不为0的第一位数字! #include #include #include using namespace std;int ans[10010];void solve(){ int i, mult; memset(ans, 0, sizeof(ans)); ans[1] = 1; mult = 1;
2012-08-15 10:58:15 862
转载 poj 1423 Big Number
题意:给出一个数字N,求N!的结果的位数。首先要求一个数字有多少位,可以用(int)log10(num)+1,这样就求出num有多少位.数N可以到10^7这么大,直接暴力的话 肯定超时。考虑下面两种方法,个人比较推荐第二种。第一种是直接打表,避免重复计算,如果要追求速度的话,可以在程序外打表,然后导入,在程序中直接查,即可,那样肯定0MS,下面的代码是在程序中打表的,所以时间爱你耗
2012-08-15 09:48:35 1751
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人