3292. 解密字符串

qqqqqcy

无while解决

因为解必存在,可以用贪心的思想。
发现英文的单词0~9有各自的特点

/* one two three four five six seven eight nine zero { 'z' is distinct }
[\zero]  one two three four five six seven eight nine {'x' is distinct }
[\six]  one two three four five seven eight nine  {'s' is distinct }
[\seven] one two three four five eight nine { 'v' is distinct }
[\five] one two three four eight nine {'f' is distinct}
[\four] one two three eight nine {'w' is distinct}
[\two] one three eight nine {'g' is distinct}
[\eight] one three nine { 'o' is distinct }
[\one] three nine {'i' is distinct}
[\nine]  three {'r' is distinct}
*/

因此我们首先保存记录每个字符
然后一步一步的取出每个数字的特征字母。
当然比如你算zero,取出zero中的z的时候,别忘了把下层的天特征字母r,o减去相应的值比如

save += table['z'];
table['r'] -= save;
table['o'] -= save;

然后用可以很轻松无while解决了。

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
int table[30];
int num[11];
int main()
{
    int cases = 0;
    scanf("%d",&cases);
    string str;

    for (int ca = 0 ; ca < cases ; ++ca)
    {
        cin >> str;
        int ans = 0 , tmp = 0;
        int sz = str.size();
        for (int i = 0 ; i < sz ; ++i)
            table[str[i] - 'A']++;

        // zero zxsvfwgoir  [zor]  
        tmp = table['Z' - 'A'];
        table['O'-'A'] -= tmp;
        table['R'-'A'] -= tmp;
        num[0] = tmp;

        //six xsvfwgoir [xis]
        tmp = table['X' - 'A'];
        table['S' - 'A'] -= tmp;
        table['I' - 'A'] -= tmp;
        num[6] = tmp;

        //seven svfwgoir [sv]
        tmp = table['S' - 'A'];
        table['V' - 'A'] -= tmp;
        num[7] = tmp;

        //five vfwgoir [vif]
        tmp = table['V' - 'A'];
        table['I' - 'A'] -= tmp;
        table['F' - 'A'] -= tmp;
        num[5] = tmp;

        //four fwgoir [for]
        tmp = table['F' - 'A'];
        table['R' - 'A'] -= tmp;
        table['O' - 'A'] -= tmp;
        num[4] = tmp;

        //two wgoir [wo]
        tmp = table['W' - 'A'];
        table['O' - 'A'] -= tmp;
        num[2] = tmp;

        //eight goir [gi]
        tmp = table['G' - 'A'];
        table['I' - 'A'] -= tmp;
        num[8] = tmp;


        //one oir [o]
        num[1] = table['O' - 'A'];

        //nine ir [i]
        num[9] = table['I' - 'A'];

        //three 
        num[3] = table['R' - 'A'];

        printf("case #%d:\n",ca);
        for (int i = 0 ; i < 10 ; ++i)
            for (int k = 0 ; k < num[i] ; ++k)
                printf("%d",i);
        printf("\n");

        memset(table,0,sizeof(table));
        memset(num,0,sizeof(num));
    }
    return 0;
}
10175102262 LarsPendragon

这一堆while看得我眼花缭乱,自己的代码好丑啊!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cmp(const void*a, const void*b)
{
    int *p1=(int*)a, *p2=(int*)b;
    return *p1-*p2;
}
int main()
{
    int T, I;
    scanf("%d",&T);
    for(I=0; I<T; I++)
    {
        char code[200];
        int i, l, cnt[26]={0}, num[100];
        scanf("%s",code);
        l=strlen(code);
        for(i=0; i<l; i++) cnt[code[i]-'A']++;
        i=0;
        while(cnt[6]>0) {cnt[6]--; cnt[4]--; cnt[7]--; cnt[8]--; cnt[19]--; num[i++]=8;}//判断8
        while(cnt[20]>0) {cnt[20]--; cnt[5]--; cnt[14]--; cnt[17]--; num[i++]=4;}//判断4
        while(cnt[23]>0) {cnt[23]--; cnt[8]--; cnt[18]--; num[i++]=6;}//判断6
        while(cnt[25]>0) {cnt[25]--; cnt[4]--; cnt[14]--; cnt[17]--; num[i++]=0;}//判断0
        while(cnt[7]>0) {cnt[7]--; cnt[4]-=2; cnt[17]--; cnt[19]--; num[i++]=3;}//判断3
        while(cnt[5]>0) {cnt[5]--; cnt[4]--; cnt[8]--; cnt[21]--; num[i++]=5;}//判断5
        while(cnt[18]>0) {cnt[18]--; cnt[4]-=2; cnt[13]--; cnt[21]--; num[i++]=7;}//判断7
        while(cnt[19]>0) {cnt[14]--; cnt[19]--; cnt[22]--; num[i++]=2;}//判断2
        while(cnt[14]>0) {cnt[14]--; cnt[4]--; cnt[13]--; num[i++]=1;}//判断1
        while(cnt[4]>0) {cnt[4]--; cnt[8]--; cnt[13]-=2; num[i++]=9;} //判断9
        qsort(num, i, sizeof(int), cmp);
        printf("case #%d:\n",I);
        for(l=0; l<i; l++) printf("%d",num[l]);
        printf("\n");
    }
    return 0;
}
Master X

你甚至可以考虑递归版本……

xidulu

//simple test of the discussion zone
//the idea is to find out all the number from the input according to a SPECIAL order

include

include

include

include

include

using namespace std;

int main()
{
int cse;
cin>>cse;
for(int p=0;p result; memset(table,0,sizeof(table)); string s; cin>>s;
for(int i=0;i<s.length();i++){
table[s[i]]++;
}
// find 0
int t=table[‘Z’];
for(int i=0;i<t;i++){
table[‘Z’]–;
table[‘E’]–;
table[‘R’]–;
table[‘O’]–;
result.push_back(0);
}
// find 6
t=table[‘X’];
for(int i=0;i<t;i++){
table[‘S’]–;
table[‘I’]–;
table[‘X’]–;
result.push_back(6);
}
// find 8
t=table[‘G’];
for(int i=0;i<t;i++){
table[‘E’]–;
table[‘I’]–;
table[‘G’]–;
table[‘H’]–;
table[‘T’]–;
result.push_back(8);
}
// find 7
t=table[‘S’];
for(int i=0;i<t;i++){
table[‘S’]–;
table[‘E’]–;
table[‘V’]–;
table[‘E’]–;
table[‘N’]–;
result.push_back(7);
}
// find 5
t=table[‘V’];
for(int i=0;i<t;i++){
table[‘F’]–;
table[‘I’]–;
table[‘V’]–;
table[‘E’]–;
result.push_back(5);
}
// find 4
t=table[‘F’];
for(int i=0;i<t;i++){
table[‘F’]–;
table[‘O’]–;
table[‘u’]–;
table[‘R’]–;
result.push_back(4);
}
// find 2
t=table[‘W’];
for(int i=0;i<t;i++){
table[‘T’]–;
table[‘W’]–;
table[‘O’]–;
result.push_back(2);
}
// find 3
t=table[‘T’];
for(int i=0;i<t;i++){
table[‘T’]–;
table[‘H’]–;
table[‘R’]–;
table[‘E’]–;
table[‘E’]–;
result.push_back(3);
}
// find 9
t=table[‘I’];
for(int i=0;i<t;i++){
table[‘N’]–;
table[‘I’]–;
table[‘N’]–;
table[‘E’]–;
result.push_back(9);
}
// find 1
t=table[‘O’];
for(int i=0;i<t;i++){
table[‘O’]–;
table[‘N’]–;
table[‘E’]–;
result.push_back(1);
}
sort(result.begin(),result.end());
cout<<”case #”<<p<<’:’<<endl;
for(int i=0;i<result.size();i++){
cout<<result[i];
}
cout<<endl;
}
}

你当前正在回复 博客/题目
存在问题!