7-4 特殊的翻译

小明的工作是对一串英语字符进行特殊的翻译:当出现连续且相同的小写字母时,须替换成该字母的大写形式,在大写字母的后面紧跟该小写字母此次连续出现的个数;与此同时,把连续的小写字母串的左侧和右侧的字符串交换位置;重复该操作,直至没有出现连续相同的小写字母为止。现在小明想请你帮他完成这种特殊的翻译。
输入格式:

输入一串由小写字母构成的字符串。(字符串长度不大于250)
输出格式:

输出翻译后的字符串。
输入样例1:

dilhhhhope

输出样例1:

在这里给出相应的输出。例如:

opeH4dil

输入样例2:

lodnkmgggggggoplerre

输出样例2:

在这里给出相应的输出。例如:

eG7lodnkmR2ople

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;cin>>s;
	int cnt=1,findit=0;
	int i=1;
	for(;i<s.length();i++){
		//cout<<s[i];
		if(s[i]==s[i-1]&&s[i]>='a'&&s[i]<='z'){
			findit=1;
			cnt++;
		}
		if(cnt==s.length()){
			cout<<(char)toupper(s[0])<<cnt;
			return 0;
		}
		if(i==s.length()-1&&!findit){
			break;
		}
		if(s[i]!=s[i-1]&&cnt>1||i==s.length()-1&&s[i]==s[i-1]){
			//cout<<endl;
			string t="";
			if(s[i]!=s[i-1])	t+=s.substr(i);
			t+=toupper(s[i-1]);
			string temp="";
			int k=cnt;
			while(k){
				temp+=(k%10+'0');
				k/=10;
			}
			reverse(temp.begin(),temp.end());
			t+=temp;
			if(s[i]==s[i-1])	t+=s.substr(0,i-cnt+1);
			else	t+=s.substr(0,i-cnt);
			s=t;
			//cout<<s<<endl;
			cnt=1;
			i=0;continue;
		}
		if(i==s.length()-1){//从头找 
			findit=0;
			i=0;
		}
	}
	cout<<s;
	return 0;
}
发布了23 篇原创文章 · 获赞 8 · 访问量 1355

猜你喜欢

转载自blog.csdn.net/qq_45550552/article/details/104884096
7-4