POJ1002: The first step in large data processing is I/O

Source: Internet
Author: User
Keywords nbsp; first step big data this

This problem is not difficult, but http://www.aliyun.com/zixun/aggregation/20522.html "> Test data (http://www.ntnu.edu.tw/acm/ProblemSetArchive /b_us_eastcen/1999/index.html) Very savage, there are several 1 million lines of test data.

Not noticing the 2000 millisecond limit at first, the results of the first version of the program were written in many functions, avoiding global variables as much as possible, and using dynamic memory allocations and struct. Later found that always timed out. Check the other people's answers online, found that you have to do a stronger input format to assume, and then do not control too many style principles, as far as possible in a main to complete. Other input and output do not use flow, the flow seems a bit slow.

A better structure on the net (http://www.cnblogs.com/mobileliker/archive/2013/05/26/3099748.html) is to open a sparse array of 10 million to save 0-9999999 of the bar chart count , eliminating the ordering of NLGN. The memory limit is not tight, there are 65536KB, this array shaping words is 40 million bytes, about 35000K bytes, so enough memory.

Another place to learn from this solution is the way he reads it, he reads it in a character, reads it in a single character, looks sloppy, but does save a single read line and iterate the repetition of each character. I/O efficiency is the key

My solution is to learn from the above method, and his solution is also more like, that is true to write is the push this method to a more extreme state.

#include "stdio.h"


int _n, _nodup = 0, _idx[10000000];


int _mapint[25] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9};


int main () {


Register int _i, _j, _x;


Register int _k;


Register char _c;


scanf ("%d\n", &_n);


for (_i = 0; _i < _n; ++_i) {


_x = 0; _j = 0;


while (_j < 7) {


_c = GetChar ();


if (_c >= ' 0 ' && _c <= ' 9 ') {


_x = _x * + _c-' 0 ';


++_j;


}else if (_c >= ' A ' && _c <= ' Y ' && _c!= ' Q ') {


_x = _x * + _mapint[_c-' A '];


++_j;


            }


        }


_idx[_x]++;


while (GetChar ()!= ' \ n ');


    }





for (_i = 0; _i < 10000000; ++_i) {


_k = _idx[_i];


if (_k > 1) {


printf ("%03d-%04d%d\n", (_i/10000), (_i%10000), _k);


_nodup = 1;


        }


    }





if (_nodup = = 0) printf ("No duplicates.\n");


return 0;


}

Edit 1: Perhaps further acceleration can be performed on the bypass output part of the remainder operator "%".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.