zoukankan      html  css  js  c++  java
  • 【replace的用法】第三届ACM/ICPC程序设计知识竞赛网络赛—— 字符串扩展

    来源:点击打开链接

    可以用replace来替换。。replace的用法中很有用的两个:

    1、用string 中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符
    用C-string 中的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符
    basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , const basic _ string& _Str , size _ type _Pos2 , size _ type );
    basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , const value _ type* _Ptr , size _ type _Num2 );

    2、用 _Count 个character _Ch , 代替操作string 中从 _Pos1 开始的 _Num1 个字符
    basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , size _ type _Count , value _ type _Ch );


    #include <iostream>
    #include <string>
    #include <ctype.h>
    using namespace std;
    
    string lowalp="abcdefghijklmnopqrstuvwxyz";
    string uppalp="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string numalp="0123456789";
    
    
    
    int main()
    {
    	int testcase;
    	string tar,res;
    	cin>>testcase;
    	while(testcase--)
    	{
    		cin>>tar;
    		int chanpos;
    		int startrep=0,enrep=0;
    		char st,ed;
    		for(int i=1;i<tar.length();i++)
    		{
    			if(tar[i]=='-')
    			{
    				chanpos=i;
    				st=tar[i-1];
    				ed=tar[i+1];
    				if(islower(st) && islower(ed) && st<ed)
    				{
    					startrep=lowalp.find(st,0);
    					enrep=lowalp.find(ed,0);
    					tar.replace(i,1,lowalp,startrep+1,enrep-startrep-1);
    					//cout<<tar<<endl;
    				}
    				else if(isupper(st) && isupper(ed) && st<ed)
    				{
    					startrep=uppalp.find(st,0);
    					enrep=uppalp.find(ed,0);
    					tar.replace(i,1,uppalp,startrep+1,enrep-startrep-1);
    					//cout<<tar<<endl;
    				}
    				else if(isdigit(st) && isdigit(ed) && st<ed)
    				{
    					startrep=numalp.find(st,0);
    					enrep=numalp.find(ed,0);
    					tar.replace(i,1,numalp,startrep+1,enrep-startrep-1);
    				}
    				else if(st==ed &&((isupper(st)&&isupper(ed))||(islower(st)&&islower(ed))||(isdigit(st)&&isdigit(ed))))
    				{
    					tar.erase(chanpos,1);				//trick
    				}
    			}	
    			
    		}
    		
    		cout<<tar<<endl;
    		
    	}
    
    	
    }
    


  • 相关阅读:
    Linux菜鸟起飞之路【三】Linux常用命令
    Linux菜鸟起飞之路【二】Linux基本常识
    Linux菜鸟起飞之路【一】基本知识与Linux的安装
    交换机和路由器区别
    netdom join more ou
    keepalive.conf配置模板
    mysql7.7.22 Gtid主从搭建
    python 列表处理
    python openpyxl模块使用
    mysql5.7
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3100506.html
Copyright © 2011-2022 走看看