acm poj-关于北大在线测试系统(POJ)第1002题

问题描述

关于北大在线测试系统(POJ)第1002题

晚辈前不久迷上了POJ,目前已经实现了这题(第1002题),并且优化到了188ms,希望高手能告知进一步优化的方法,我希望能进入100ms以内,多谢前辈们指点!

目前我已经知道性能瓶颈是fgets()这个函数上,它大概花了和排序相同的时间
scanf(),gets()我都试过了,效率没有fgets()高
而fread()在读取stdin的时候又没办法及时地跳出来,所以没法使用
(比如说,用fread从一个只有1000字节的文件中读取10000个字节,那么在读到文件尾后会自动地跳出来。而在读stdin的时候,并不存在所谓的文件尾,它以为你还要不停地输入下去)
排序我这里用的是STL里的sort()函数,应该是非常高效的。

附上代码:

#include
#include
#include
#include

int main (int argc, char * argv[])
{
int n;
char TelTemp[50];
int TelTable[100000];
int i;
int j;

char symbol_table[100];

int Frequency = 1;
int have_result = 0;

symbol_table['0'] = 0;
symbol_table['1'] = 1;
symbol_table['A'] = symbol_table['B'] = symbol_table['C'] = symbol_table['2'] = 2;
symbol_table['D'] = symbol_table['E'] = symbol_table['F'] = symbol_table['3'] = 3;
symbol_table['G'] = symbol_table['H'] = symbol_table['I'] = symbol_table['4'] = 4;
symbol_table['J'] = symbol_table['K'] = symbol_table['L'] = symbol_table['5'] = 5;
symbol_table['M'] = symbol_table['N'] = symbol_table['O'] = symbol_table['6'] = 6;
symbol_table['P'] = symbol_table['R'] = symbol_table['S'] = symbol_table['7'] = 7;
symbol_table['T'] = symbol_table['U'] = symbol_table['V'] = symbol_table['8'] = 8;
symbol_table['W'] = symbol_table['X'] = symbol_table['Y'] = symbol_table['9'] = 9;

scanf ("%d", &n);
getchar();

for (i = 0; i < n; ++i)
{
    int times = 6;
    TelTable[i] = 0;

    fgets (TelTemp, 50, stdin);

    for (j = 0; j < 50; ++j)
    {
        if (TelTemp[j] != '-')
        {
            TelTable[i] = TelTable[i] * 10 + symbol_table[TelTemp[j]];
            --times;
            if (times < 0)
            {
                break;
            }
        }
    }
}

std::sort (TelTable, TelTable + n);

for (i = 0; i < n - 1; ++i)
{
    if (TelTable[i] == TelTable[i + 1])
    {
        ++Frequency;
    }
    else
    {
        if (Frequency >= 2)
        {
            printf ("%03ld-%04ld %dn", TelTable[i] / 10000, TelTable[i] - TelTable[i] /10000 * 10000, Frequency);
            Frequency = 1;
            have_result = 1;
        }
    }
}
if (Frequency >= 2)
{
    printf ("%03ld-%04ld %dn", TelTable[i] / 10000, TelTable[i] - TelTable[i] /10000 * 10000, Frequency);
    Frequency = 1;
    have_result = 1;
}
if (have_result == 0)
{
    printf ("No duplicates.");
}
return 0;

}

时间: 2024-12-03 21:57:57

acm poj-关于北大在线测试系统(POJ)第1002题的相关文章

c语言-【编程题】替换空格,在线测试系统显示程序异常退出

问题描述 [编程题]替换空格,在线测试系统显示程序异常退出 题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 我的编程思想是先统计空格个数blankCount,由此算出替换后的字符串长度tLength,后来通过下标从后面往前面替换,这样遇到空格插入%20,否则将字符后移. 我的程序: class Solution { public: void replaceSpace(c

acm-zoj ACM 1002题,运行显示runtime err

问题描述 zoj ACM 1002题,运行显示runtime err 本机用例测试正确,请问是哪里出问题了呢 import java.util.ArrayList; import java.io.BufferedInputStream; import java.util.Scanner; public class Main { public static int MaxNum=0; public static ArrayList MaxNumArray=new ArrayList(); priv

poj 3164 Command Network:最小树形图模板题

链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS     Memory Limit: 131072K Total Submissions: 8922     Accepted: 2609 Description After a long lasting war on words, a war on arms finally breaks out between littleken's and Knu

poj 1330 Nearest Common Ancestors:LCA入门题

链接: http://poj.org/problem?id=1330 题目: Nearest Common Ancestors Time Limit: 1000MS     Memory Limit: 10000K Total Submissions: 12678     Accepted: 6764 Description A rooted tree is a well-known data structure in computer science and engineering. An e

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

HDU 2818 Building Block, poj 1988 Cube Stacking:带权并查集

链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=2818 POJ:  http://poj.org/problem?id=1988 题目: Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N.Initially, there are N piles, and each pile contai

算法题之poj 1961 Period(KMP, 最短循环节)

题目链接: POJ  : http://poj.org/problem?id=1961 HDU : http://acm.hdu.edu.cn/showproblem.php?pid=1358 ZOJ   : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2177 题目大意: 给定一个长度为n的字符串s,求它的每个前缀的最短循环节.换句话说,对于每个i (2<=i<=n),求一个最大的整数K>1(如果K存在),使

poj 1456 Supermarket:贪心, 并查集

链接: http://poj.org/problem?id=1456 题目: Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the

poj 1679 The Unique MST:次小生成树

链接: http://poj.org/problem?id=1679 题目: Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of