vector容器的reverse函数的使用
题目:B. Reversing Encryptiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA string s of length n can be encrypted by the following algorithm: itera
题目:
A string s
can be encrypted by the following algorithm:
- iterate over all divisors of n
- ).
For example, the above algorithm applied to the string s
=" codeforces" leads to the following changes: " codeforces" → " secrofedoc" → " orcesfedoc" → " rocesfedoc" → " rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1).
You are given the encrypted string t
. Your task is to decrypt this string, i.e., to find a string s such that the above algorithm results in string t. It can be proven that this string salways exists and is unique.
The first line of input consists of a single integer n
, and it consists only of lowercase Latin letters.
Print a string s
大体意思就是不停地对称,从第一个开始,隔一个,以当前的这个字符为对称轴,两边同时旋转,一直到中间停止。
一开始我是用char做的,打了打之后,发现对称的时候,两边的字符串得到长度必须相等才能对称,比如说 12345,你要以2为对称轴进行对称时,2的左边就很难弄,所以这个之后可以用string,string函数中还带有很多强大的库函数。
代码如下:
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
bool cmp(int t1,int t2){
return t1<t2;
}
int main(){
vector<int>wakaka;
int n;
cin>>n;
wakaka.clear();
string s;
cin>>s;
for(int i=1;i<=n;i++){
if(n%i==0){
wakaka.push_back(i);
}
}
sort(wakaka.begin(),wakaka.end(),cmp);
int len=wakaka.size();
for(int i=0;i<len;i++){
int t=wakaka[i];
reverse(s.begin(),s.begin()+t);//字符反转函数
}
cout<<s<<endl;
return 0;
}
更多推荐
所有评论(0)