洛谷P1098 字符串的展开

这个题稍不注意就是WA

  1. 考虑‘ - ’出现在首尾的情况
  2. 考虑例如‘ - - x ’的情况
  3. 不同为字母或数字则不展开

代码:

        #include<bits/stdc++.h>
        using namespace std;
        int p1, p2, p3;
        string s;
        const string a = "abcdefghijklmnopqrstuvwxyz";
        const string A = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int seek(char x) {
        for(int i = 0; i < 26; i ++) 
             if(x == a[i] || x == A[i])
             return i; } 
        map<int,char>f;
        string P3(string s, int a, int b) {
             if(p3 == 2) for(int i = a; i <= (a+b)/2; i ++)
                           swap(s[i],s[a+b-i]);
                           return s;} //是否逆序
         int main() {
              cin >> p1 >> p2 >> p3 >> s;
              int len = s.length() ;
              string ss;
              int P2 = p2;
              int t = 0, now, ge, num = 0;
              f[0] = '0',f[1] = '1',f[2] = '2',f[3] = '3';
              f[4] = '4',f[5] = '5',f[6] = '6',f[7] = '7';
              f[8] = '8',f[9] = '9';
              for(int i = 0; i < len; i ++) {
	                if(s[i] == '-' && i != 0 && i != len-1) {
		               if(s[i-1] - '0' <= s[i+1] -'0' && s[i-1] != '-' && s[i+1] != '-') {
			                  if(s[i-1]-'0' <= 9 && s[i+1]-'0' > 9)
			                      ss += s[i];
		 	                  else if(s[i-1] -'0' + 1 == s[i+1] -'0') {
				                  num --;
				                  continue;}
			        else if(s[i-1] - '0' == s[i+1] - '0')  {  //保留-
			    	    ss += s[i];
			    	    continue;}
			        else if(s[i-1] - '0' > 9 && s[i+1] - '0' > 9) { //字母 
				         ge = 0;
				         now = i + num - t; 
				         t ++;
					for(int j = seek(s[i-1])+1; j < seek(s[i+1]); j ++){
					    while(P2 --) {
					    	if(p1 == 1) ss += a[j];
						    else if(p1 == 2) ss += A[j];
						    else ss += '*';
						    ge ++;
						    num ++;}
					    P2 = p2;}   
				    ss = P3(ss, now, now+ge-1);}
			else if(s[i-1] - '0' <= 9 && s[i+1] - '0' <= 9) { //数字
			        ge = 0;
			        now = i + num - t;
			        t ++;
					for(int j = s[i-1] - '0' + 1; j < s[i+1] - '0'; j ++){
					    while(P2 --) {
					       if(p1 == 3) ss += '*';
						   else ss += f[j];
						   ge ++;
						   num ++;}  
						P2 = p2;}
					ss = P3(ss, now, now+ge-1); }}
		else ss += s[i]; }
	else ss += s[i];}
cout << ss << endl;
return 0;}

WA了好多发o(╥﹏╥)o
o(╥﹏╥)oo(╥﹏╥)oo(╥﹏╥)o
而且还感jio自己代码特别繁琐
好多东西都不知道
。。。。。。。。。
下次想偷懒就来看看o(╥﹏╥)o

稍微好了一点儿的代码:

 #include<bits/stdc++.h>
 using namespace std; 
 int main() {
     int p1, p2, p3;
     string s, ss;
     cin >> p1 >> p2 >> p3 >> s;
     cout << s[0];
     for(int i = 1; i < s.length() - 1; i ++) {
	       if(s[i] == '-') {
	           if(s[i-1] + 1< s[i+1]) {
		            if((s[i-1] >= 'a' && s[i+1] <= 'z') || (s[i-1] >= '0' && s[i+1] <='9')) {
			            char now = s[i-1] + 1;
			                ss = "";
			            int P2 = p2;
			            while(now < s[i+1]) {
				                while(P2 --) {
				                    if(p1 == 1 || p1 == 2) ss += now;
				                    else ss += '*';}
		                        now += 1, P2 = p2;}
		              if(p1 == 2) transform(ss.begin(), ss.end(), ss.begin(), ::toupper); 
			if(p3 == 1) for(int j = 0; j < ss.length(); j ++)
		                     cout << ss[j];
		    else for(int j = ss.length()-1; j >= 0; j --)
		                     cout << ss[j];}
	         else cout << "-"; }
	  else if(s[i-1] + 1 == s[i+1]) continue;
	  else cout << '-';} 
	else cout << s[i];}
cout << s[s.length()-1];
return 0;}

关于string的大小写的转化:
transform(s.begin(), s.end(), s.begin(), ::toupper);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值