3238. 字串非重复字符数排序

Canis

各种睿智bug,写了我两个小时才ac

include

include

int compare(char a,int lena, char b, int lenb)
{
//前态:(句子a,a的长度,句子b, b的长度)
//a大就返回1,a小就返回-1,相等返回0
int max;
if (lena < lenb)
max = lenb;
for(int i = 0; i < max; i++)
{
if(a[i] > b[i])
return 1;
if(a[i] < b[i])
return -1;
}
return 0;
}

int differ(char* a, int lena)//计算不同字符数
{
int q[21] = {0};//用于查重
int num = 0;
for(int i = 0; i < lena; i++)
{
int flag = 0;
for(int j = 0; j < lena; j++)
{
if (a[i] == q[j] )
flag = 1;
}
if (flag == 0)
num++;
q[i] = a[i];
}
return num;
}

int main()
{

int t;
scanf("%d", &t);//共有几组问题
int w = 0;
while (t--)
{
    int n;
    scanf("%d", &n);//每组有几个句子
    char a[n][21];
    int a1[n] ;//存放每个句子的不同字符数
    for (int i = 0; i < n; i++){//获取字符串
        scanf("%s", &a[i]);
        getchar();
        a1[i] = differ(a[i], strlen(a[i]));
        //记录不同字符数
    }





    for(int i = 0; i < n - 1; i++)
    {                 //i为已经排序好的数的数量
        for(int j = 0; j < n - i - 1; j++)
        {    //j为排序的下标
            if(a1[j] < a1[j+1])
             {
                 char b[21];
                 strcpy(b, a[j]);
                 strcpy(a[j], a[j+1]);
                 strcpy(a[j+1], b);
                 int t = a1[j];
                 a1[j] = a1[j+1];
                 a1[j+1] = t;
             }
        }
    }
    for (int i = 0; i < n; i++){
        a1[i] = differ(a[i], strlen(a[i]));
    }
    for(int i = 0; i < n; i++)
    {                 //i为已经排序好的数的数量
        for(int j = 0; j < n - i - 1; j++)
        { //j为排序的下标
            if(a1[j] == a1[j+1])
             {
                 if(compare(a[j], strlen(a[j]), a[j+1], strlen(a[j+1])) > 0)
                 {
                     char b[21];
                     strcpy(b, a[j]);
                     strcpy(a[j], a[j+1]);
                     strcpy(a[j+1], b);

                 }

             }
        }
    }
    printf("case #%d:\n", w);
    for(int i = 0; i < n; i++)
    {
        printf("%s\n",a[i]);
    }
    w++;

}


return 0;

}

Andrew-Malcom

include

using namespace std;
int judge(string a)
{
        int i,num[26]={0},sum=0;
        for(i=0;i<a.size();i++){
                num[a[i]-'A']++;
        }
        for(i=0;i<26;i++){
                if(num[i]!=0) sum++;
        }
        return sum;
}
bool cmp(string a,string b)
{
        if(judge(a)!=judge(b)){
                return judge(a)>judge(b);
        }
        else{
                return a<b;
        }
}
int main()
{
        int t,i;
        scanf("%d",&t);
        for(i=0;i<t;i++){
                vector<string>str;
                int n;
                cin>>n;
                int j,k,m;
                for(j=0;j<n;j++){
                        string s;
                        cin>>s;
                        str.push_back(s);
                }
                printf("case #%d:\n",i);
                sort(str.begin(),str.end(),cmp);
                for(string kk:str){
                        cout<<kk<<endl;
                }

        }
}
一串萌萌哒的代码

include

include

int main(){
int t,d=0;
scanf(“%d”,&t);
while(t>0){
int x[100],n,k,i,j;
scanf(“%d”,&n);
char str[100][20];
for(i=0;i<n;i++){
scanf(“%s”,str[i]);
x[i]=1;
for(j=1;j<strlen(str[i]);j++){
for(k=0;k<=j-1;k++){
if(str[i][j]==str[i][k]){
break;
}else if(k==j-1){
x[i]++;
}
}
}
}

    for(i=n-1;i>=1;i--){
        for(j=1;j<=i;j++){
           if(x[j]>x[j-1]){
                 char shu[20];
                 strcpy(shu,str[j]);
                 strcpy(str[j],str[j-1]);
                 strcpy(str[j-1],shu);
                 int p=x[j-1];x[j-1]=x[j];x[j]=p;
           }else if(x[j]==x[j-1]){
              if(strcmp(str[j],str[j-1])<0){
                 char shu[20];
                 strcpy(shu,str[j]);
                 strcpy(str[j],str[j-1]);
                 strcpy(str[j-1],shu);
                 int p=x[j-1];x[j-1]=x[j];x[j]=p;
              }
           }
        }
    }
    printf("case #%d:\n",d);
    for(i=0;i<=n-1;i++){
       puts(str[i]);
    }
    t--;d++;
}

}

Commando
#include <bits/stdc++.h>

using namespace std;

using pairsi = pair<string, int>;

int findUnique(const string& s) {
    unordered_map<char, bool> hashmap;
    for (auto const& ch : s)  hashmap[ch] = true;

    return static_cast<int>(hashmap.size());
}

bool comp(const pairsi& lhs, const pairsi& rhs) {
    return lhs.second == rhs.second ? lhs.first < rhs.first : lhs.second > rhs.second;
}

int main() {
    int T;
    cin >> T;
    for (int i = 0; i < T; ++i) {
        int n;
        cin >> n;
        vector<pairsi> v;
        while (n--) {
            string s;
            cin >> s;
            v.emplace_back(make_pair(s, findUnique(s)));
        }
        sort(v.begin(), v.end(), comp);

        printf("case #%d:\n", i);
        for (auto const& p : v) cout << p.first << endl;
    }
}
kukusugar
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct hello{
    string s;
    int p;
}str[110];
bool cmp(hello a,hello b){
    if(a.p!=b.p)return a.p>b.p;
    else return a.s<b.s;
}
int main(){
    int n,cs,m,i,j,mx,count;
    int flag[26]={},sign[110]={};
    cin>>n;
    for(cs=0;cs<n;cs++){
        cin>>m;
        mx=0;
        for(i=0;i<m;i++){
            cin>>str[i].s;
        }
        for(i=0;i<m;i++){
            count=0;
            for(j=0;j<str[i].s.size();j++){
                if(flag[str[i].s[j]-'A']==0){
                    flag[str[i].s[j]-'A']=1;
                    count++;
                }
            }
            for(j=0;j<26;j++){
                flag[j]=0;
            }
            str[i].p=count;
        }
        sort(str,str+m,cmp);
        cout<<"case #"<<cs<<":"<<endl;
        for(i=0;i<m;i++){
            cout<<str[i].s<<endl;
        }

    }
}
Fifnmar

C 语言版本:

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

typedef uint32_t u32;
typedef int32_t i32;

typedef struct {
    char str[21];
    i32 digit_cnt;
} Rec;

int cmp(void const *restrict lhs, void const *restrict rhs) {
    return ((Rec *)lhs)->digit_cnt == ((Rec *)rhs)->digit_cnt
               ? strcmp(((Rec *)lhs)->str, ((Rec *)rhs)->str)
               : ((Rec *)rhs)->digit_cnt - ((Rec *)lhs)->digit_cnt;
}

int main() {
    u32 t; scanf("%u", &t);
    for (u32 i = 0; i < t; ++i) {
        printf("case #%u:\n", i);
        u32 n; scanf("%u", &n);
        Rec recs[100];
        for (u32 i = 0; i < n; ++i) {
            scanf("%s", recs[i].str);
            recs[i].digit_cnt = 0;
            // 26 letters take one bit indivisually.
            u32 bit_cnt = 0;
            for (char *j = recs[i].str; *j; ++j) {
                u32 bit = 1 << (*j - 'A');
                if ((bit_cnt & bit) == 0)
                    bit_cnt |= bit,
                    ++recs[i].digit_cnt;
            }
        }
        qsort(recs, n, sizeof(Rec), cmp);
        for (u32 i = 0; i < n; ++i)
            puts(recs[i].str);
    }
}
Fifnmar

合理组织结构可以方便操作。如果数据量再大一点,可以考虑对索引排序而不是直接排序(交换的代价较高)。

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

int main() {
    uint32_t t; cin >> t;
    for (uint32_t query = 0; query < t; ++query) {
        uint32_t n; cin >> n;
        struct Rec {
            uint32_t ch_num = 0;
            string val;
        };
        Rec strs[100];
        for (uint32_t i = 0; i < n; ++i) {
            cin >> strs[i].val;
            unordered_set<char> cnt;
            for (auto ch : strs[i].val)
                cnt.insert(ch);
            strs[i].ch_num = cnt.size();
        }
        sort(strs, strs + n, [](Rec const &a, Rec const &b) {
            if (a.ch_num == b.ch_num)
                return a.val < b.val;
            else
                return a.ch_num > b.ch_num;
        });
                cout << "case #" << query << ":\n";
        for (uint32_t i = 0; i < n; ++i)
            cout << strs[i].val << '\n';
    }
}
cww970329
#include <bits/stdc++.h>
using namespace std;
const int N = 111;

bool vis[333]; // visited

struct node{
    string st;
    int cnt; // count

    void read(){
        cin >> st;
        cnt = 0;
        //memset(vis, 0, sizeof vis);
        for (int i = 0; i < 333; i++) vis[i] = 0;
        for (int i = 0; i < st.length(); i++){
            if (vis[st[i]] == 1) continue;
            vis[st[i]] = 1;
            cnt++;
        }
    }

    bool operator < (const node &b) const{
        if (cnt == b.cnt) return st < b.st;
        return cnt > b.cnt;
    }
} arr[N];


int main(){
    //freopen("in.txt", "r", stdin);
    int __;
    scanf("%d", &__);
    for (int n, _ = 0; _ < __; _++){
        scanf("%d", &n);
        for (int i = 0; i < n; i++){
            arr[i].read();
        }
        sort(arr, arr + n);
        printf("case #%d:\n", _);
        for (int i = 0; i < n; i++){
            cout << arr[i].st << endl;
        }
    }
    return 0;
}
wwh66258

include

include

include

typedef struct{
char str[21];
int sum;
}data;
int cmp(const void a,const void b){
data s1=(data)a;
data s2=(data)b;
if(s1->sum==s2->sum)return strcmp(s1->str,s2->str);
else return s2->sum-s1->sum;
}
int main(){
int t;
scanf(“%d”,&t);
for(int wwh=0;wwh<t;wwh++){
int n;
scanf(“%d”,&n);
data ch=(data)malloc(n*sizeof(data));
for(int i=0;i<n;i++){
scanf(“%s”,ch[i].str);
int len=strlen(ch[i].str);
int code[26]={0};
for(int j=0;j<len;j++){
code[ch[i].str[j]-‘A’]++;
}
ch[i].sum=0;
for(int j=0;j<26;j++){
if(code[j]!=0)ch[i].sum++;
}
}
qsort(ch,n,sizeof(data),cmp);
printf(“case #%d:\n”,wwh);
for(int i=0;i<n;i++){
printf(“%s\n”,ch[i].str);
}
}
return 0;
}

lnu_cxn

哪位大佬可以说一下这个代码怎么才能显示正常

lnu_cxn

include

include

include

include

using namespace std;
struct word {
string s;
int num;
};
bool cmp(const word& a, const word& b) {
if (a.num != b.num)
return a.num > b.num;
return a.s < b.s;
}
int main() {
int t, count = 0;
cin >> t;
while (t–) {
int n;
cin >> n;
vector g(n);
for (int i = 0; i < n; i++) {
string temp;
cin >> temp;
set look;
for (int j = 0; j < temp.size(); j++)
look.insert(temp[j]);
g[i].s = temp;
g[i].num = look.size();
}
sort(g.begin(), g.end(), cmp);
cout << “case #” << count++ << “:\n”;
for (int i = 0; i < n; i++)
cout << g[i].s << endl;
}
return 0;
}

woshenaide

include

include

include

char alpha[26]= {‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’,’N’,
‘O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’
};
void swap(char str1,char str2);
int main()
{
int m;
scanf(“%d”,&m);
for(int i1=0; i1<m; i1++)
{
int n;
scanf(“%d”,&n);
char str[n][30];
int con[n];
for(int c1=0; c1<n; c1++)
{
con[c1]=0;
}

    for(int j1=0; j1<n; j1++)
    {
        int flag[26]= {0};
        scanf("%s",str[j1]);
        int len=strlen(str[j1]);
        for(int i2=0; i2<26; i2++)
        {
            for(int k1=0; k1<len; k1++)
            {
                if(str[j1][k1]==alpha[i2])
                {
                    flag[i2]=1;
                }
            }
        }

        for(int i3=0; i3<26; i3++)
        {
            if(flag[i3]==1)
            {
                con[j1]++;
            }
        }
    }


    for(int j2=0; j2<n; j2++)
    {
        for(int k2=j2+1; k2<n; k2++)
        {
            if(con[j2]<con[k2])
            {
                char temp[30];
                strcpy(temp,str[j2]);
                strcpy(str[j2],str[k2]);
                strcpy(str[k2],temp);
                int tem;
                tem=con[j2];
                con[j2]=con[k2];
                con[k2]=tem;
            }
            if(con[j2]==con[k2])
            {
                if(strcmp(str[j2],str[k2])>0)
                {
                    char temp[30];
                    strcpy(temp,str[j2]);
                    strcpy(str[j2],str[k2]);
                    strcpy(str[k2],temp);
                }
            }
        }
    }
    printf("case #%d:\n",i1);
    for(int j3=0; j3<n; j3++)
    {
        printf("%s",str[j3]);
        printf("\n");
    }
}
return 0;

}

Sanity

include

include

include

typedef struct
{
char s[1001];
char c[1001];
int differ;
} Array;

int cmp(const voida, const voidb)
{
int r;
Arrayaa=(Array)a;
Arraybb=(Array)b;
return (r=bb->differ-aa->differ)?r:strcmp(aa->s,bb->s);
}

int cmp1(const voida,const voidb)
{
return (char)a-(char)b;
}

int main()
{
int T,t,n,i,j;
Array a[1001];
scanf(“%d”,&T);
for(t=0; t<T; t++)
{

    scanf("%d",&n);

    for(i=0; i<n; i++)
    {
        scanf("%s",a[i].s);
        a[i].differ=0;
        strcpy(a[i].c,a[i].s);
    }
    for(i=0; i<n; i++)
    {
        qsort(a[i].s,strlen(a[i].s),1,cmp1);
        for(j=0; j<strlen(a[i].s); j++)
        {
            if(a[i].s[j]!=a[i].s[j+1])
                a[i].differ+=1;
        }
    }
    qsort(a,n,sizeof(Array),cmp);
    printf("case #%d:\n",t);
    for(i=0; i<n; i++)
        printf("%s\n",a[i].c);
}
return 0;

}
测试数据都对,调试也没什么问题,就是一直WA,有没有大神能指导一下啊233

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