P1098 [NOIP 2007 提高组] 字符串的展开
P1098 [NOIP 2007 提高组] 字符串的展开 - 洛谷
#include<bits/stdc++.h>
using namespace std;
int p1,p2,p3;
string s,tmp;
bool pan(char a,char b)
{return (a>='0'&&a<='9' && b>='0' && b<='9'|| (a>='a'&&a<='z'&&b>='a'&&b<='z'));
}
char PP1(char c)
{if(p1==3) return '*';if(p1==2 && islower(c)) return toupper(c);return tolower(c);
}
string PP2(char c)
{return string(p2,c);
}
int main()
{cin>>p1>>p2>>p3>>s;string res;res+=s[0];for(int i=1;i<s.size()-1;tmp="",i++){if(s[i]=='-' && pan(s[i-1],s[i+1]) && s[i+1]>s[i-1]){vector<char> mid;for(char c=s[i-1]+1;c<s[i+1];c++)mid.push_back(c);if(p3==2) reverse(mid.begin(),mid.end());for(char c:mid){char t=PP1(c);res+=PP2(t);}}elseres+=s[i];}res+=s.back();cout<<res;return 0;
}
知识点
if(p1==2 && islower(c)) return toupper(c);return tolower(c);
大小写
. string(p2, '*')
-
这是 C++ 的
std::string
构造函数用法,表示:创建一个由
p2
个*
组成的字符串。 -
举例:如果
p2 = 3
,就会创建"***"
。