洛谷 P1098 [NOIP2007 提高组] 字符串的展开 题解 C/C++ 模拟

模拟就好了 坑比较多
1.当‘-’两端的字符分别是数字和字母时,原样输出。
2.字符串首尾的‘-’需要原样输出。
3.连续的‘-’需要原样输出。
4.存放展开字符串的数组开大点

附几组样例:
输入:
2 4 2
a-b-c
输出:
abc

输入:
2 2 2
9-a-a
输出:
9-a-a

输入:
1 5 1
-254-243-52-345-243-5234-52-345-234-52-345-234-52345-4325-2345-2345-2345
输出:
-254-2434444452345-24344444523452345-23452345-23452345-4325-2345-2345-2345

输入:
1 1 1
2-43-sdf-ewr0-j-9re-j0g-9e0-9as-d09jf-9asdjf0q-w-ejr0q-59jdsnf-9z-x9v0-sd9fn--q
输出:
2343-sdf-ewr0-j-9refghij0g-9e0123456789as-d09jf-9asdjf0qrstuvw-ejr0q-59jdsnf-9z-x9v0-sd9fn--q

输入:
2 8 2
--09-8-w-er-7h-08w-e7-hc-r890-q7w-eh-rc98-07-q8-ewr-8h-c-8-294-5-dsf--k-h-2-48-3k-h-sd-fq-a-
输出:
--09-8-w-er-7h-08w-e7-hcQQQQQQQQPPPPPPPPOOOOOOOONNNNNNNNMMMMMMMMLLLLLLLLKKKKKKKKJJJJJJJJIIIIIIIIHHHHHHHHGGGGGGGGFFFFFFFFEEEEEEEEDDDDDDDDr890-q7w-ehQQQQQQQQPPPPPPPPOOOOOOOONNNNNNNNMMMMMMMMLLLLLLLLKKKKKKKKJJJJJJJJIIIIIIIIrc98-07-q8-ewr-8h-c-8-2945-dsf--k-h-23333333348-3k-hRRRRRRRRQQQQQQQQPPPPPPPPOOOOOOOONNNNNNNNMMMMMMMMLLLLLLLLKKKKKKKKJJJJJJJJIIIIIIIIsdEEEEEEEEfq-a-
绝了 恶心

//P1098 [NOIP2007 提高组] 字符串的展开
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>//abs
#include <cctype>//isdigit
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
char str[105],buf[10500];
int len,idx;
void strUnfold(int p,int p2,int p3) {
    
    
	idx = 0; 
	int ptr = 0,step;
	for(int i = 0;i<len;i++) {
    
    
		if(str[i]!='-') {
    
    
			buf[idx++] = str[i];
			ptr = idx;
			continue;
		}
		else {
    
    
			if((i==len-1&&str[i]=='-')||(i==0&&str[i]=='-')||(str[i+1]=='-'||str[i-1]=='-')||(isdigit(str[i-1])&&isalpha(str[i+1]))||(isdigit(str[i+1])&&isalpha(str[i-1]))||(str[i-1]>=str[i+1])) {
    
    
				buf[idx++] = '-';
				continue;
			}

			step = str[i+1]-str[i-1]-1;
			int k = p2;
			if(p==3) {
    
    
				for(int j = 1;j<=step*k;j++) {
    
    
					buf[idx++] = '*';
				}
			}
			else if(isdigit(str[i-1])&&p!=3) {
    
    
				int flag = 0;
				for(int j = 1;j<=step;j++) {
    
    
					for(int m = 1;m<=k;m++) {
    
    
						buf[idx++] = str[i-1]-'0'+j+48;
					}
					flag = 1;	
				}
				if(p3==2&&flag) {
    
    //逆序
					int t = 1;
					for(int q = ptr;q<(idx+ptr)/2;q++) {
    
    
						char tmp;
						tmp = buf[q];
						buf[q] = buf[idx-t];
						buf[idx-t] = tmp;
						t++;
					}
				}

			}
			else if(isalpha(str[i-1])) {
    
    
				int flag = 0;
				if(p==1) {
    
    
					for(int j = 1;j<=step;j++) {
    
    
						for(int m = 1;m<=k;m++) {
    
    
							buf[idx++] = str[i-1]-'a'+j+97;
						}
						flag = 1;					
					}
					if(p3==2&&flag) {
    
    //逆序
						int t = 1;
						for(int q = ptr;q<(idx+ptr)/2;q++) {
    
    
							char tmp;
							tmp = buf[q];
							buf[q] = buf[idx-t];
							buf[idx-t] = tmp;
							t++;
						}
					}

				}
				else if(p==2) {
    
    
					for(int j = 1;j<=step;j++) {
    
    
						for(int m = 1;m<=k;m++) {
    
    
							buf[idx++] = str[i-1]-'a'+j+97-32;
						}
						flag = 1;
					}
					if(p3==2&&flag) {
    
    //逆序
						int t = 1;
						for(int q = ptr;q<(idx+ptr)/2;q++) {
    
    
							char tmp;
							tmp = buf[q];
							buf[q] = buf[idx-t];
							buf[idx-t] = tmp;
							t++;
						}
					}

				}
			}	
		}
		
		
	}

}


int main() {
    
    
#ifdef LOCAL
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
#endif
	int p1,p2,p3;
	cin>>p1>>p2>>p3;
	scanf("%s",str);
	len = strlen(str);
	strUnfold(p1,p2,p3);
	for(int i = 0;i<idx;i++) {
    
    
		cout<<buf[i];
	}
}

猜你喜欢

转载自blog.csdn.net/Jason__Jie/article/details/115050667