从结果中删除重复项

时间:2011-12-31 22:11:54

标签: c++ duplicates

我有这个代码创建一个范围单词,我的问题是当我想从'abcd'(最小范围= 4,最大范围= 4)创建一个范围单词时,它创建'cdaa'或'ccca'和。 ..我想要删除重复的'aa'或'cc'并且......有什么办法吗?这可以添加一个代码到我的程序,删除这样的副本?


mycode:

#include <string.h>
#include <windows.h>
#include <stdio.h>
#include <math.h>

typedef unsigned long long int64;


// config
char configfile[] = "settings.ini";
char outputfile[ 1<<7 ];
char charset[ 1 << 9 ];
int maxPWLength = 5;

// local vars
int cursorPosition = 0;
int offsetCursor = 0;
int currentLength = 1;
char currentword[ 1<<7 ];
int64 estfilesize = 0;
int64 charswritten = 0;
int lastpercentage = 0;
FILE *fp;

int64 pow64(unsigned int base, unsigned int exp)
{
    int64 result = 1;
    for (unsigned int v=0; v < exp; v++)
        result *= base;

    return result;
}


void Processbar()
{
    int percentage = (charswritten * 100) / estfilesize;
    if (percentage > 100) percentage = 100;
    char bar[51] = ""; ZeroMemory(bar, sizeof(bar));
    if (percentage > lastpercentage)
    {
        lastpercentage = percentage;
        int bars = percentage / 2;
        for (int z=0; z <= bars; z++)
            bar[z] = '|';

        printf("\r       [ %-50.50s ]  [ %d%% ]", bar, percentage);
    }
}
void Writeword(char* word)
{
    fprintf(fp, "%s\n", currentword);
    charswritten += strlen(word) + 2;
    Processbar();
}

BOOL GeneratePosition(int position)
{
    for (unsigned int x=0; x < strlen(charset); x++)
    {
        currentword[position] = charset[x];
        if (position > 0) GeneratePosition(position-1);
        if (((position > 0) && (currentword[position] != charset[0])) || (position == 0))   Writeword(currentword);
    }
    if (cursorPosition == maxPWLength-1) return FALSE;

    return TRUE;
}

int main(int argc, char* argv[])
{
    // -> header
    printf("                                                                         \n");
    printf("      #################################################################  \n");
    printf("     #                                                                 # \n");
    printf("     #             word List Generator by sh4d0w` v1.8             # \n");
    printf("     #                                                                 # \n");
    printf("      #################################################################  \n");
    printf("                                                                         \n");
    printf("                                                                         \n");


    // -> main routine

    // parse configuration file
    char path[ 1<<9 ]; GetCurrentDirectory( sizeof(path)-1, path);
    char absconfigfile[ 1<<10 ]; sprintf(absconfigfile, "%s\\%s", path, configfile);
    GetPrivateProfileString("PWListGen", "outputfile", "words.txt", outputfile, sizeof(outputfile)-1, absconfigfile);
    GetPrivateProfileString("PWListGen", "charset", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", charset, sizeof(charset)-1, absconfigfile);
    maxPWLength = GetPrivateProfileInt("PWListGen", "maxPWLength", 4, absconfigfile);
    cursorPosition = GetPrivateProfileInt("PWListGen", "minPWLength", 1, absconfigfile)-1;

    if (maxPWLength < cursorPosition+1) maxPWLength = cursorPosition+1;

    for (int x=0; x<=cursorPosition; x++)
        currentword[x] = charset[0]; // set startword

    // calculate estimated filesize
    /*double estfilesize = 0;
    for (int x=1; x <= maxPWLength; x++)
        estfilesize += (pow( strlen(charset), x) * (x+2));*/ // inaccurate

    for (int x=1; x <= maxPWLength; x++)
        estfilesize += pow64(strlen(charset), x) * (x+2); // combinations * length of combination and \r\n

    char estfilesizestr[ 1<<7 ] = "";
    if (estfilesize < 1024LL)
        sprintf(estfilesizestr, "%.2f Bytes", (double)estfilesize);
    else if (estfilesize < pow64(1024,2)) {
        sprintf(estfilesizestr, "%.2f kBytes", (double)estfilesize / 1024LL); }
    else if (estfilesize < pow64(1024,3))
        sprintf(estfilesizestr, "%.2f MBytes", (double)estfilesize / pow64(1024,2));
    else if (estfilesize < pow64(1024,4))
        sprintf(estfilesizestr, "%.2f GBytes", (double)estfilesize / pow64(1024,3));
    else if (estfilesize < pow64(1024,5))
        sprintf(estfilesizestr, "%.2f TBytes", (double)estfilesize / pow64(1024,4));
    else sprintf(estfilesizestr, "too big");

    // open filehandle
    if (fp = fopen(outputfile, "w"))
    {
        printf(" + Config:\n\n");
        printf("   -  output file = %.48s\n", outputfile);
        printf("   -  charset = [%.256s]\n", charset);
        printf("   -  min. word length = %d\n", cursorPosition+1);
        printf("   -  max. word length = %d\n", maxPWLength);
        printf(" \n");


        printf(" + Estimated file size: %.48s\n\n", estfilesizestr);

        printf(" + Opened %.48s successfully... Attempting to write now...\n\n", outputfile);

        // doing that generating process
        while (GeneratePosition(cursorPosition))
            cursorPosition++;

        // close filehandle
        fclose(fp);
        printf("\n\n + Done. \n\n");

        getchar();

    }


    return 0;
}

setting.ini:

[WListGen]
outputfile=word.txt
charset=abcd
maxPWLength=4
minPWLength=4

1 个答案:

答案 0 :(得分:0)

只需添加QP功能,在运行void Writeword(char * word)之前,检查currentword, 使用

if(QP(currentword,strlen(currentword))) 

这样的一些代码。

 bool QP(char A[],int n)  //delete duplicate 
{
   for(int i=0;i<n-1;i++)
    { 
        if(A[i+1]==A[i])
           return false;
    }
   return true;
}

void Writeword(char* word)
{
  if(QP(currentword,strlen(currentword)))
    {
     fprintf(fp, "%s\n", currentword);
     charswritten += strlen(word) + 2;
     Processbar();
    } 
}