pku 1256 Anagram

这题和pku1731 Orders差不多。

//法1 63MS
#include<iostream>
#include <algorithm>
#include<string>

using namespace std;

char str[15];    //存储输入字符串
char out[15];    //存储输出字符串
bool used[15];        //字符是否已经使用
int N;            //字符个数
//比较两个字符的大小

int cmp(const void * a,const void *b)
{
    char c=*(char *)a;
    char d=*(char *)b;
    if(c>=97)
    {
        c-=32;
        if(d>=97)
        {
            d-=32;
            return c-d;
        }
        else
        {
            if(c==d)
                return 1;
            else
                return c-d;
        }
    }
    else
    {
        if(d>=97)
        {
            d-=32;
            if(c == d)
                return -1;
            else
                return c-d;
        }
        return c-d;
    }
}
//回溯
void backtrace(int n)
{
    if(n==N)//最后一层,输出字符串
    {
        printf("%s\n",out);
    }
    else
        for(int i=0; i<N; ++i)
        {
            if(used[i]==false )
            {
                out[n]=str[i];
                used[i]=true;
                backtrace(n+1);

                used[i]=false;

                int k=i+1;
                //跳到第一个不相同的字符
                while(k<N && str[k]==str[i] )
                    k++;
                k--;//循环结束时i会自增,先减1
                i=k;
            }
        }
}

int main()
{
    int t,T;
    scanf("%d",&T);
    for(t=0; t<T; ++t)
    {
        scanf("%s",str);
        N=strlen(str);
        memset(used,0,sizeof(used));
        out[N]='\0';
        //对输入字符进行排序
        qsort(str,N,sizeof(str[0]),cmp);
        backtrace(0);
    }
    return 0;
}

 

//****************************************************

 

//法2 hash做法 63MS
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

#define SIZE 15
#define HASHSIZE 52

char str[SIZE],out[SIZE];
int len,hash[HASHSIZE];

void dfs(int cur)
{
    if(cur==len)
    {
        out[cur]=0;
        printf("%s\n",out);
    }
    else
        for(int i=0; i<HASHSIZE; i++)
        {
            if(hash[i]>0)
            {
                //  out[cur]='A'+i;//////////////wrong
                if(i&1)//单数,即小写字母
                {
                    out[cur]=(i-1)/2+'a';
                }
                else//双数,大写字母
                {
                    out[cur]=i/2+'A';
                }
                hash[i]--;
                dfs(cur+1);
                hash[i]++;
            }
        }
}

int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%s",str);
        len=strlen(str);
        memset(hash,0,sizeof(hash));
        for(int i=0; i<len; i++)
        {
            //  hash[ str[i]-'A' ]++;//wrong

            //小写a在A之后
            if(str[i]>='a')
            {
                //小写字母hash为 3 5 7 9 11 13 15 ...50
                //               a b c d  e  f  g ... z
                hash[ (str[i]-'a')*2 + 1 ]++;
            }
            else
            {
                //大写字母hash为 0 2 4 6 8 10 12...51
                //               A B C D E  F  G ...Z
                hash[ (str[i]-'A')*2 ]++;
            }
        }
        dfs(0);
    }
    return 0;
}

 

//****************************************************

 

//法3 266MS

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

char s[15],ch[15],out[15];
int n,k;
bool use[15];

void dfs(int p)
{
    char last=-1;
    for(int i=0; i<n; i++)
    {
        if(use[i]) continue;
        if(last==s[i]) continue;//避免重复
        last=s[i];
        out[p]=s[i];
        use[i]=true;
        if(p<n-1)
            dfs(p+1);
        else
        {
            for(int i=0; i<n; i++)
                printf("%c",out[i]);
            printf("\n");
        }
        use[i]=false;
    }
}
int main()
{
    int t,i,j;
    char c1,c2,temp;
    scanf("%d",&t);
    while(t--)
    {
        memset(use,false,sizeof(use));
        scanf("%s",s);
        n=strlen(s);
        for(i=0; i<n; i++)
            for(j=i+1; j<n; j++) //用来排序
            {
                if(s[i]<='Z')
                    c1=s[i]+'a'-'A';
                else c1=s[i];

                if(s[j]<='Z')
                    c2=s[j]+'a'-'A';
                else c2=s[j];

                if(c1>c2||(c1==c2&&s[i]>s[j]))
                {
                    temp=s[i];
                    s[i]=s[j];
                    s[j]=temp;
                }
            }
        dfs(0);
    }
    return 0;
}

 

posted @ 2010-08-18 13:42  菜到不得鸟  阅读(212)  评论(0)    收藏  举报