zoukankan      html  css  js  c++  java
  • 字符串的展开 vijos1379 NOIP2007 字符串 模拟

    题面在最下方。

    一道简单模拟题,做完之后应该大声喊一句STL大法好,string大法好!

    带你们复习一下string的基本函数

    string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
    int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置,查找成功返回位置,查找失败返回-1(据说是返回的string::npos的值,但我发现好像都是-1)
    string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s

    同时,string库为string类重载了运算符,可以直接用 += 来为string字符串末尾加上字符或字符串。

    附上AC代码

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<string>
     5 #include<iostream>
     6 using namespace std;
     7 template<class T> inline void read(T &_a){
     8     bool f=0;int _ch=getchar();_a=0;
     9     while(_ch<'0' || _ch>'9'){if(_ch=='-')f=1;_ch=getchar();}
    10     while(_ch>='0' && _ch<='9'){_a=(_a<<1)+(_a<<3)+_ch-'0';_ch=getchar();}
    11     if(f)_a=-_a;
    12 }
    13 
    14 int p1,p2,p3,loc,fr=1;
    15 string str,tmp;
    16 
    17 inline bool check()
    18 {
    19     return (str[loc-1]>='a'&&str[loc-1]<='z'&&str[loc+1]>='a'&&str[loc+1]<='z'&&str[loc-1]<str[loc+1])||(str[loc-1]>='0'&&str[loc-1]<='9'&&str[loc+1]>='0'&&str[loc+1]<='9'&&str[loc-1]<str[loc+1]);
    20 }
    21 
    22 int main()
    23 {
    24     read(p1); read(p2); read(p3);
    25     cin>>str;
    26     while(loc=str.find('-',fr))
    27     {
    28         if(loc==-1) break;
    29         fr=loc+1;
    30         if(!check()) continue;
    31         tmp="";
    32         if(p1==3)
    33         {
    34             for(register int i=str[loc-1]+1;i<str[loc+1];++i)
    35                     for (register int v=1;v<=p2;++v)
    36                         tmp+='*';
    37         } else if(str[loc-1]>='a')
    38         {
    39             if(p1==1)
    40             {
    41                 for(register int i=str[loc-1]+1;i<str[loc+1];++i)
    42                     for (register int v=1;v<=p2;++v)
    43                         tmp+=char(i);
    44             } else {
    45                 for(register int i=str[loc-1]+1;i<str[loc+1];++i)
    46                     for (register int v=1;v<=p2;++v)
    47                         tmp+=char(i-'a'+'A');
    48             }
    49         } else {
    50             for(register int i=str[loc-1]+1;i<str[loc+1];++i)
    51                     for (register int v=1;v<=p2;++v)
    52                         tmp+=char(i);
    53         }
    54         if(p3==2&&p1!=3)
    55         {
    56             int len=tmp.length()-1;
    57             string tmp2="";
    58             for (register int i=len;i>=0;--i)
    59                 tmp2+=tmp[i];
    60             tmp=tmp2;
    61         }
    62         str.replace(loc,1,tmp);
    63     }
    64     cout<<str;
    65     return 0;
    66 }
    View Code

    描述

    在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的

    字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续

    递增的字母获数字串替代其中的减号,即,将上面两个子串分别输出为“defgh”和“45678”。在

    本题中,我们通过增加一些参数的设置,使字符串的展开更为灵活。具体约定如下:

    1. 遇到下面的情况需要做字符串的展开:在输入的字符串中,出现了减号“-”,减号两侧同为小写字母或同为数字,且按照ASCII码的顺序,减号右边的字符严格大于左边的字符。
    2. 参数p1:展开方式。p1=1时,对于字母子串,填充小写字母;p1=2时,对于字母子串,填充大写字母。这两种情况下数字子串的填充方式相同。p1=3时,不论是字母子串还是数字字串,都用与要填充的字母个数相同的星号“*”来填充。
    3. 参数p2:填充字符的重复个数。p2=k表示同一个字符要连续填充k个。例如,当p2=3时,子串“d-h”应扩展为“deeefffgggh”。减号两边的字符不变。
    4. 参数p3:是否改为逆序:p3=1表示维持原来顺序,p3=2表示采用逆序输出,注意这时候仍然不包括减号两端的字符。例如当p1=1、p2=2、p3=2时,子串“d-h”应扩展为“dggffeeh”。
    5. 如果减号右边的字符恰好是左边字符的后继,只删除中间的减号,例如:“d-e”应输出为“de ”,“3-4”应输出为“34”。如果减号右边的字符按照ASCII码的顺序小于或等于左边字符,输出 时,要保留中间的减号,例如:“d-d”应输出为“d-d”,“3-1”应输出为“3-1”。

    格式

    输入格式

    包括两行:
    第1行为用空格隔开的3个正整数,一次表示参数p1,p2,p3。

    第2行为一行字符串,仅由数字、小写字母和减号“-”组成。行首和行末均无空格。

    输出格式

    只有一行,为展开后的字符串。

    样例1

    样例输入1

    1 2 1
    abcs-w1234-9s-4zz

    样例输出1

    abcsttuuvvw1234556677889s-4zz

    限制

    1s

  • 相关阅读:
    大魔头视频解析 线路收集(持续更新)
    如何配置双网?
    pom配置资源文件中的二进制文件乱码打不开如excel
    漏洞复现-CVE-2017-7525-Jackson远程代码执行
    漏洞复现-CVE-2018-1000861-jenkins远程命令执行
    漏洞复现-wooyun-2015-110216-Elasticsearch写入webshell
    漏洞复现-CVE-2015-5531-ElasticSearch 目录穿越
    漏洞复现-nginx_parsing_vulnerability-nginx解析漏洞
    漏洞复现-insecure-configuration-nginx不安全的配置
    漏洞复现-CVE-2018-1273-Spring Data Commons 远程命令执行
  • 原文地址:https://www.cnblogs.com/jaywang/p/7698799.html
Copyright © 2011-2022 走看看