2012年4月2日

c++比c快?一道字符串题目

摘要: 题目是这样的:求通过添加字将一个字符串变为回文字符串所需最少的添加次数。解法:求出该字符串与反串的最大公共子序列的长度k,那么字符串的长度n减去k就是所求值。为了不超出内存限制,使用了类似滚动数组的方法。问题在这里,C程序和改写后的C++程序(只改写了头文件,添加了一句using namespace std;),而被判运行时差足有3s,有图为证:可能与测试数据和OJ的评判系统有关,但我想肯定也和这两种语言某种方面的差异有关,在此请教路过的同学,谢谢!附:原题链接:1097 PalindromeC代码 1 # include <stdio.h> 2 # include <str 阅读全文

posted @ 2012-04-02 22:22 getgoing 阅读(567) 评论(0) 推荐(0) 编辑

UVa 357 - Let Me Count The Ways

摘要: 又是硬币型的。。虽然通过了,还有几个疑问:1. 题目中说 The input will consist of a set of numbers between 0 and 30000 inclusive, one per line in the input file. , 难道不包括30000吗? 输入30000时AC的程序结果是负值(将long long int 改为unsigned long long int可以解决)。2. 使用dev c++输出时出现错误: There are 6 ways to produce 0 cents change. There are 4 ways t... 阅读全文

posted @ 2012-04-02 13:18 getgoing 阅读(351) 评论(0) 推荐(0) 编辑

UVa 147 - Dollars

摘要: 无数次WA换来一次AC,只因没有使用long long int。。。 1 # include <stdio.h> 2 # include <math.h> 3 4 const int coin[] = {0,5,10,20,50,100,200,500,1000,2000,5000,10000}; 5 6 long long int f[6010][12]; 7 8 long long int dp(int m, int i); 9 10 int main()11 {12 double c;13 14 while (1)15 {16 sca... 阅读全文

posted @ 2012-04-02 11:47 getgoing 阅读(288) 评论(0) 推荐(0) 编辑

UVa 10192 - Vacation

摘要: 又是lcs,所幸 7min AC了。 1 # include <stdio.h> 2 # include <string.h> 3 4 int len; 5 int x[2]; 6 char s1[105]; 7 char s2[105]; 8 int ans[105*105]; 9 10 int lcs(int *x);11 12 int main()13 {14 int cnt;15 16 cnt = 0;17 while (NULL != gets(s1))18 {19 if (s1[0] == '#') break;20... 阅读全文

posted @ 2012-04-02 10:22 getgoing 阅读(380) 评论(0) 推荐(0) 编辑

UVa 10066 - The Twin Towers

摘要: 一看就知道是lcs,10分钟敲好了代码,通过示例,一提交MLE,明白了没有保存中间结果;再次提交,打开邮箱,一直F5,等了很长时间没反应,难道又MLE了?结果回到题目页面,发现了下面这句话:You have to select a programming language.。。。再次提交WA:没有把ans[]初始化为-1;最终花了25分钟才AC了,教训啊! 1 # include <stdio.h> 2 # include <memory.h> 3 4 int N1, N2; 5 int x[2]; 6 int s1[101]; 7 int s2[101]; 8 int 阅读全文

posted @ 2012-04-02 10:06 getgoing 阅读(552) 评论(0) 推荐(0) 编辑

UVa 10131 - Is Bigger Smarter?

摘要: DAG最大路径;AC后我搜了搜,有几个是理解为最大下降子列(我开始也这么想的),但是作为dp的练习题,就化为DAG了,复杂度当然高了,但是没超时。 1 # include <stdio.h> 2 3 int n; 4 int w[1000]; 5 int s[1000]; 6 int d[1000]; 7 int g[1000][1000]; 8 9 int dp(int i);10 void print_list(int i);11 12 int main()13 {14 int i, j, maxl, k;15 16 n = 1;17 while (~s... 阅读全文

posted @ 2012-04-02 09:30 getgoing 阅读(378) 评论(0) 推荐(0) 编辑

UVa 116 - Unidirectional TSP

摘要: 简单的动态规划(递推),要能够打印最小权值对应的最小字典序路径;MIN 定义中‘<’写成了‘>’导致一次WA。。另外求最小权值对应的最小下标那里写的很乱。# include <stdio.h># define MIN(X,Y) ((X)<(Y) ? (X):(Y))int f[12][105];int p[12][105];int col, row;int main(){ int i, j, ans, t, x; while (~scanf("%d%d", &row, &col)) { for (i = 0; i < ro 阅读全文

posted @ 2012-04-02 00:50 getgoing 阅读(1311) 评论(0) 推荐(0) 编辑

导航