Poj 3320 Jessica's Reading Problem(尺取法)
来源:互联网 发布:中世纪纪录片知乎 编辑:程序博客网 时间:2024/05/16 11:43
做这题的时候因为忽视了一个条件而吃了一发RE,下面先把RE代码和AC代码都贴出来,然后再进行分析。
RE代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n , a[1000005] , b[1000005] ;int main(void){while ( scanf ("%d",&n ) != EOF ){int num = 0 ;memset ( b , 0 , sizeof ( b ) );for ( int i = 0 ; i < n ; i ++ ){scanf ("%d",&a[i] );if ( !b[a[i]] ){b[a[i]] ++ ;num ++ ;}}int s = 0 , e = 0 , l = n + 1 , sum = 0 ;memset ( b , 0 , sizeof ( b ) );for ( int i = 0 ; i < n ; i ++ ){if ( b[a[i]] == 0 )sum ++ ;b[a[i]] ++ ;e ++ ;while ( sum == num ){l = min ( l , e - s );b[a[s]] -- ;if ( !b[a[s]] )sum -- ;s ++ ;}}printf ("%d\n",l );} return 0;}
AC代码:
#include <map>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n , a[1000005] ;map<int,int> b;int main(void){while ( scanf ("%d",&n ) != EOF ){int num = 0 ;b.clear();for ( int i = 0 ; i < n ; i ++ ){scanf ("%d",&a[i] );if ( !b[a[i]] ){b[a[i]] ++ ;num ++ ;}}int s = 0 , e = 0 , l = n + 1 , sum = 0 ;b.clear();for ( int i = 0 ; i < n ; i ++ ){if ( b[a[i]] == 0 )sum ++ ;b[a[i]] ++ ;e ++ ;while ( sum == num ){l = min ( l , e - s );b[a[s]] -- ;if ( !b[a[s]] )sum -- ;s ++ ;}}printf ("%d\n",l );} return 0;}
两段代码的差别在于:RE代码单纯使用数组,而AC代码使用了STL中的map,原因是:在我处理后面的操作时,是将某一数值作为下标,记录其出现次数的,而题目中input有这样一句话:You may assume all integers that appear can fit well in the signed 32-bit integer type.
因此该数值可能超出所定义数组的下标最大值,导致RE。因此将数组的使用改为map的使用,来解决这一问题。
阅读全文
0 0
- POJ 3320 Jessica's Reading Problem(尺取法)
- POJ - 3320 Jessica's Reading Problem(尺取法)
- poj 3320 Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem(尺取法)
- poj 3320 Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem (HASH, 尺取法)
- POJ 3320 Jessica's Reading Problem (尺取法)
- Jessica's Reading Problem(POJ No.3320) (尺取法)
- 【POJ】3320--Jessica's Reading Problem(尺取法)
- Poj 3320 Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem 尺取法
- POJ 3320 Jessica's Reading Problem (尺取法)
- poj 3320 Jessica’s Reading Problem 【尺取法】
- poj 3320 Jessica's Reading Problem 【尺取法+STLmap set】
- 安装Ubuntu双系统问题记录
- VMware Workstation12 虚拟机破解版(附永久秘钥)
- linux内核编译
- 合并两个排序的链表
- P1020 导弹拦截
- Poj 3320 Jessica's Reading Problem(尺取法)
- Redis学习-3.4:Set集合详解
- java编程思想贰(多态到数组)
- ng-pattern 不能含有空格及特殊字符
- 进程饥饿和死锁
- vim 的一般模式
- 解决Eclipse部署Web项目在Tomcat Webapps 目录中找不到
- 用会 MySQL LIMIT
- Redis的哨兵模式