POJ 1002

过程是 先读入字符串,里面会有大写字母,根究要求转换成数字,然后对转换后的进行排序,把重复两次以上的都输出出来。


代码如下:


#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <list>
#include <string>
#include<cstring>

using namespace std;

//#define fin cin
//#define fout cout

int cp(const void *a,const void *b)
{
 return (*(const string *)a).compare(*(const string *)b);
}


string l[100000];
string s;


int main()
{
    int n;


    ifstream fin("input.txt");
    ofstream fout("output.txt");


    fin>>n;


    if ( n == 0 ){
        fout<<"No duplicates."<<endl;
        return 0;
    }


    int countl = 0;


    while(n){


        fin >> s;


        for(int i = 0; i <= s.length() - 1; i++){
            if( s.at(i) == '-'){
                s.erase(i, 1);
                i--;
                continue;
            }
            if( s.at(i) >= 'A' && s.at(i) <= 'O')
                s.at(i) = (s.at(i) - 'A') / 3 + 2 + '0';
            else if( s.at(i) >= 'P' && s.at(i) <= 'S')
                s.at(i) = '7';
            else if( s.at(i) > 'S')
                s.at(i) = (s.at(i) - 'T') / 3 + 8 + '0';
        }
        l[countl++] = s.c_str();
        n--;
    }

    qsort(l, countl , sizeof(l[0]), cp);

    string it = l[1];
    bool isDup = false;

    int i = 0;

    string lastString = l[0];


    if( countl == 1){
        fout<<"No duplicates."<<endl;
        return 0;
    }
    else{
        for(i = 1; i < countl ; i++){
            it = l[i];
            int dupNum = 1;
            while( i != countl && it == lastString){
                isDup = true;
                dupNum++;
                it=l[++i];
            }
            if (dupNum != 1){
	            lastString.insert(3,"-");
                fout<<lastString<<" "<<dupNum<<endl;
            }
            lastString = it;


        }
    }

    if (!isDup)
        fout<<"No duplicates."<<endl;

    return 0;
}

总结有:

1002

字母转换成对应数字,排序,统计重复后输出

  1. 思路是把字母变换成数字后,用int村上,然后对整数排序,最后处理成字符串,加上“-”输出。
  2. 思路问题:一定要考虑整数第一位为0时候怎么办!!要么最后输出的时候处理出来,要么就直接处理字符串排序!搞的全乱了, 要换思路!
  3. 包含stdlib.h后,就可以使用sscanf,来进行整数到字符串的变换,可以用atoi来进行字符串到整数的变换,也可以用sprintf来进行字符串到整数的变换。不过据说atoiitoaPOSIT标准的,只在VC6下好使,也有说都好使的。
  4. s.find("-") != string::npos 查找不到的条件, 是不等于string::npos
  5. while( it != v.end() && *it == lastInt)这种语句,一定要把!=end()写在前面,不然会出错的!
  6. qsort函数的使用,第一个参数是要sort的数组,第二个是数组大小,第三个是数组每个元素的大小,第四个是一个比较函数!!
  7. 一个比较函数可以这么写:

intcmp(const void* a,const void* b)

{

    return *(int*)a-*(int*)b;

}这样的函数可以传给qsort,或者使用strcmp;主要是要传入两个void*指针,传出一个int

  1. 最后把代码的容器给去掉了,应为listsort函数,超时了,还是qsort比较靠谱。
  2. main()函数外面定了数组string a[100000],没有必要动态申请,因为怎么样都要承受这个范围的,在main()函数外面定义更好,分配空间的地方不一样,局部变量是放在堆栈段的,而全局变量是放在数据段。
  3. 后面放了一个看到的比较牛人写的。。。。 以后也可以像他这样弄一个表,用起来方便。划分逻辑还麻烦些





/* Copyright Derek Kisman (ACM ICPC ECNA 1999) */  
  
char map[26] = {'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', 0};  
  
char ph[100000][9];  
int nph;  
  
char buf[1000];  
  
main() {  
    int i, j, k, x, y, z, n;  
    char ch;  
  
    memset( ph, 0, sizeof(ph) );  
    scanf( " %d", &nph );  
    for( i = 0; i < nph; i++ ) {  
        scanf( " %s", buf );  
        x = 0;  
        for( j = 0; buf[j]; j++ ) {  
            if( buf[j] == '-' ) continue;  
            if( buf[j] >= 'A' && buf[j] <= 'Z' ) buf[j] = map[buf[j]-'A'];  
            ph[i][x++] = buf[j];  
            if( x == 3 ) ph[i][x++] = '-';  
        }  
    }  
    qsort( ph, nph, 9, strcmp );  
    x = 1; z = 0;  
    for( i = 1; i < nph; i++ ) {  
        if( strcmp( ph[i-1], ph[i] ) ) {  
            if( x > 1 ) {  
                printf( "%s %d\n", ph[i-1], x );  
                z = 1;  
            }  
            x = 1;  
        } else {  
            x++;  
        }  
    }  
    if( x > 1 ) {  
        printf( "%s %d\n", ph[i-1], x );  
        z = 1;  
    }  
    if( !z ) printf( "No duplicates.\n" );  
}  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值