Gym - 102082A 字符串的比较(模拟)

Problem A   Digits Are Not Just Characters 

题意:

给你若干个文件名,每个文件名由大写字母(从“A”到“Z”)、小写字母(从“A”到“Z”)和数字(从“0”到“9”)组成。每个大写或小写字母看作成一个字母项。每个连续的数字序列构成一个数字项。(例X52Y,"52"是单独的一项,5和2都不是。)

比较规则:1、数字项在字母项之前。2、两个字母项按ASCII码排序,小的在前,大的在后。3、当比较的两项都为数字项时,按从小到大排序。4、从顶部开始,逐项比较两个文件名,第一个不同对应项的顺序决定文件名的顺序。如果其中一个A,比另一个B有更多的项,并且B的所有项都和A对应的项相同,那么项少的B应该在A前面。

输入: 第一行 n为要与s0比较的数目,第二行s0是被比较的串,接下来n行是要进行比较的n个字符串。

输出: 对于每个文件名,从s1到sn,输出一行,其中的字符表示是否应该在s0之前。如果要在s0之前列出字符,则字符应该是“-”;否则,它应该是“+”,包括两个名称相同的情况。

 

根据题意模拟即可,组队赛的时候没做出来,下面的代码都是别人的题解,四种方法都能过,我自己写的太垃圾,不放在博客里面了。

题解1:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
string s,s1;
bool judge()
{
    int len1=s.length();
    int len2=s1.length();
    int flag=0;
    for(int i=0;i<len1&&i<len2;i++)
    {
        if(isalpha(s[i])&&isalpha(s1[i]))//都是字母的情况
        {
            if(s[i]>s1[i])
            {
                flag=-1;
                break;
            }
            else if(s[i]<s1[i])
            {
                flag=1;
                break;
            }
            else if(s[i]==s1[i])
                continue;
        }
        else if(isalpha(s[i])||isalpha(s1[i]))
        {
            if(!isalpha(s[i]))
            {
                flag=1;
                break;
            }
            else
            {
                flag=-1;
                break;
            }
        }
        else if(isdigit(s[i])&&isdigit(s1[i]))
        {
            int k=i;
            int j=i;
            int num1=0,num2=0;
            while(k<len1&&isdigit(s[k]))
            {
                num1=num1*10+(s[k]-'0');
                k++;
            }
            while(j<len2&&isdigit(s1[j]))
            {
                num2=num2*10+(s1[j]-'0');
                j++;
            }
//            cout<<num1<<" "<<num2<<endl;
            if(num1>num2)
            {
                flag=-1;
                break;
            }
            else if(num1<num2)
            {
                flag=1;
                break;
            }
            else continue;
        }
    }
    if(flag==-1)
        return false;
    else if(flag==1)
        return true;
    else
    {
        if(len1>len2)
            return false;
        else
            return true;
    }
}
int main()
{
    int n;
    ios::sync_with_stdio(false);
//    freopen("i.txt","r",stdin);
    while(cin>>n)
    {
        cin>>s;
        for(int i=0;i<n;i++)
        {
            cin>>s1;
            if(judge())
                cout<<"+"<<endl;
            else
                cout<<"-"<<endl;
        }
    }
    return 0;
}

题解2:


#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<ctype.h>
using namespace std;
bool check(string a,string b)
{
    for(int i=0;i<a.length();i++)
    {

        if(i>=b.length())
            return true;
        if(isdigit(a[i]))
        {
            if(!isdigit(b[i]))
                return false;
            else //都是数字的情况
            {
                int t=i;
                long long num1=0,num2=0;
                while(t<a.length()&&isdigit(a[t]))
                {
                    num1=num1*10+(a[t]-'0');
                    t++;
                }
                t=i;
                while(t<b.length()&&isdigit(b[t]))
                {
                    num2=num2*10+(b[t]-'0');
                    t++;
                }
                if(num1<num2) return false;
                if(num2<num1) return true;

            }
        }
        else//a不是数字的情况
        {
            if(isdigit(b[i])) return true;
            if(a[i]<b[i]) return false;
            if(a[i]>b[i]) return true;
        }
    }
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    freopen("i.txt","r",stdin);
    string a,b;
    int n;
    cin>>n;
    cin>>a;
    for(int i=0;i<n;i++)
    {
        cin>>b;
        puts(check(a,b)?"-":"+");
    }

    return 0;
}

题解3:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define cls(x) memset(x,0,sizeof(x))
#define clslow(x) memset(x,-1,sizeof(x))

const int maxn=100;

string get(string a,int& index)
{
    string str="";
    int alen=a.length();
    bool flag=isdigit(a[index]);
    for(int i=index;i<alen;i++){
        if(isdigit(a[i])==flag){
            str+=a[i];
        }
        else{
            index=i;
            return str;
        }
    }
    index=alen;
    return str;
}

int judge(string a,string b)
{
    int aindex=0,bindex=0;
    int alen=a.length(),blen=b.length();
    while(aindex<alen&&bindex<blen)
    {
        string stra=get(a,aindex);
        string strb=get(b,bindex);

//        cout<<stra<<" "<<strb<<endl;

        if(isdigit(stra[0])&&isalpha(strb[0]))  return -1;
        else if(isalpha(stra[0])&&isdigit(strb[0])) return 1;

        if(isdigit(stra[0])){
            if(stra.length()<strb.length()) return -1;
            else if(stra.length()>strb.length())    return 1;
        }
        for(int i=0;i<stra.length();i++){
            if(stra[i]==strb[i])    continue;
            else if(stra[i]<strb[i])    return -1;
            else return 1;
        }
        if(isalpha(stra[0])){
            if(stra.length()<strb.length()) return -1;
            else if(stra.length()>strb.length())    return 1;
        }
    }
    if(aindex<alen) return 1;
    else if(bindex<blen)    return -1;
    return 0;
}

int main()
{
//    freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n)
    {
        string s0;
        cin>>s0;
        for(int i=1;i<=n;i++){
            string str;
            cin>>str;
            int val=judge(s0,str);
            if(val<=0)   cout<<'+'<<endl;
            else        cout<<'-'<<endl;
        }
    }
    return 0;
}

题解4:

#include <stdio.h>
#include<iostream>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
bool cmp(string x,string y)
{
    string x1,y1;
    int p,q;
    int i=0,j=0;
    while(i<x.length()&&j<y.length())
    {
        p=0,q=0;
        x1="",y1="";
        int b;
        if(x[i]>='0'&&x[i]<='9')
        {
            b=1;
            while(x[i]>='0'&&x[i]<='9')
            {
                x1+=x[i];
                i++;
            }
            int l=x1.length();
            for(int k=l-1;k>=0;k--)
            {
                p+=(x1[k]-'0')*b;
                b*=10;
            }
            x1="";
        }
        else if(x[i]>='A'&&x[i]<='z')
        {
            while(x[i]>='A'&&x[i]<='z')
            {
                x1+=x[i];
                i++;
            }
        }

        if(y[j]>='0'&&y[j]<='9')
        {
            b=1;
            while(y[j]>='0'&&y[j]<='9')
            {
                y1+=y[j];
                j++;
            }
            int l=y1.length();
            for(int k=l-1;k>=0;k--)
            {
                q+=(y1[k]-'0')*b;
                b*=10;
            }
            y1="";
        }
        else if(y[j]>='A'&&y[j]<='z')
        {
            while(y[j]>='A'&&y[j]<='z')
            {
                y1+=y[j];
                j++;
            }
        }
        if(p!=0&&q!=0)
        {
            if(p>q)
                return true;
            else if(p==q)
                continue;
            else
                return false;
        }
        if(x1!=""&&y1!="")
        {
            if(x1>y1)
                return true;
            else if(x1==y1)
                continue;
            else
                return false;
        }
        if(x1!=""&&q!=0)
            return true;
        if(y1!=""&&p!=0)
            return false;
    }
    if(i>=x.length()&&j>=y.length())
        return false;
    if(i<x.length())
        return true;
    if(j<y.length())
        return false;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int n;
    while(~scanf("%d",&n))
    {
        string s0;
        cin>>s0;
        string si;
        for(int i=1; i<=n; i++)
        {
            cin>>si;
            if(cmp(s0,si))
                printf("-\n");
            else
                printf("+\n");
        }
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值