洛谷1098 字符串的展开

本文提供了一道洛谷平台上的字符串模拟题目解答,通过详细的代码示例讲解了如何处理字符串转换的问题,包括字符比较、条件判断及特殊字符处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题地址

https://www.luogu.org/problem/show?pid=1098

字符串 模拟

解题思路

一道模拟题,然而如果没有练熟字符串,打起来会相当痛苦……其实本质就是暴力if if if,注意一下三个参数的判断顺序,没有很坑的地方,耐心打总能打完的。

参考代码(相当冗长仅供参考)

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
     strings,c[10086];
     intp1,p2,p3;
     scanf("%d%d%d",&p1,&p2,&p3);
     cin>>s;
     intcnt=0,st=0;
     for(int i=0;i<s.length();i++)
     {
         if(s[i]=='-')
         {
              stringss=s.substr(st,i-st);
              cnt++;
              c[cnt]=ss;
              st=i+1;
         }
     }
     cnt++;
     c[cnt]=s.substr(st,s.length()-st);
     charw[10086],t[10086];
     inta[10086],b[10086];
     for(int i=1;i<=cnt;i++)
     {
         charp=c[i][c[i].length()-1];
         if(p<='z'&&p>='a') b[i]=1;
         if(p<='Z'&&p>='A') b[i]=2;
         if(p<='9'&&p>='0') b[i]=3;
         p=c[i][0];
         if(p<='z'&&p>='a') a[i]=1;
         if(p<='Z'&&p>='A') a[i]=2;
         if(p<='9'&&p>='0') a[i]=3;
         t[i]=c[i][0];
         w[i]=c[i][c[i].length()-1];
     }
     stringans=c[1];
     intnum;
     charch;
     for(int i=1;i<cnt;i++)
     {
         if(b[i]!=a[i+1])
         {
              ans=ans+'-'+c[i+1];
              continue;
         }
         if(b[i]==2||a[i]==2)
         {
              ans=ans+'-'+c[i+1];
              continue;
         }
         if(w[i]>=t[i+1])
         {
              ans=ans+'-'+c[i+1];
              continue;
         }
         if(w[i]==t[i+1]+1)
         {
            ans=ans+c[i+1];
            continue;
         }
         if(b[i]==1)
         {
              if(p1==1)
              {
                   ch=w[i]+1;
                   while(ch<t[i+1])
                   {
                       for(int j=1;j<=p2;j++)
                       ans+=ch;
                       ch++;
                   }
              }
              if(p1==2)
              {
                   ch=w[i]-'a'+'A'+1;
                   while(ch<t[i+1]-'a'+'A')
                   {
                       for(int j=1;j<=p2;j++)
                       ans+=ch;
                       ch++;
                   }
              }
              if(p1==3)
              {
                   num=t[i+1]-w[i]-1;
                   for(int j=1;j<=num*p2;j++) ans+='*';
              }
         }
         if (b[i]==3)
         {
            if(p1==3)
            {
                 num=t[i+1]-w[i]-1;
                 for(int j=1;j<=num*p2;j++) ans+='*';
              }else
              {
                   ch=w[i]+1;
                   while(ch<t[i+1])
                  {
                     for(int j=1;j<=p2;j++)
                     ans+=ch;
                     ch++;
                   }
              }
         }
         if (p3==1)
         {
              ans+=c[i+1];
              continue;
         }
         num=(t[i+1]-w[i]-1)*p2;
         int k=ans.length();
         stringss=ans.substr(k-num,num);
         for(int j=0;j<num/2;j++)
              swap(ss[j],ss[ss.length()-1-j]);
         ans=ans.substr(0,k-num)+ss;
         ans+=c[i+1];
     }
     cout<<ans;
     return0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值