Codeforces Round #490 (Div. 3)B. Reversing Encryption

1,题目描述:

B,反转加密
一个长度为n的字符串s可以用下面的算法进行加密。
*降序遍历n的所有因数(从n到1)
*对于每个因数d,倒置子串s[1……d].
例如,利用上面的算法对字符串s=“codeforces”进行转化。“codeforces”->”secrofedoc”
->”orecsfedoc”->”rocesfrdoc”->”rocesfedoc”(显而易见的,最后的转换操作并没有改变字符串因为此时的因数为d=1)
给你一个已经倒置的字符串t,你的任务是还原这个字符串,可以保证这个字符串一定是存在的而且是唯一的。
输入:
第一行是一个整数n(1~100)字符串t的长度。第二行是字符串t,字符串的长度为n,它只包含小写字母。
输出:
根据结果t的转换的算法得到s并输出

2,题目链接:

http://codeforces.com/contest/999/problem/B

3,通过的代码:

  1. 自己的代码:
#include<iostream>
using namespace std;
int main(){
    int n;cin>>n;
    string str;cin>>str;
    int a[102],cnt=0;
    for(int i=1;i<=n;i++)if(n%i==0)a[++cnt]=i;
    for(int i=1;i<=cnt;i++){
        string s;
        for(int j=a[i]-1;j>=0;j--)s+=str[j];
        for(int j=a[i];j<n;j++)s+=str[j];
        str=s;
    }
    cout<<str<<endl;
} 
  1. 大神的代码:
#include<cstdio>
#include<algorithm>
using namespace std;const int N=107;
char s[N];int n,i,j;
int main(){
    for(scanf("%d%s",&n,s+1),i=1;i<=n;++i)if(n%i==0)reverse(s+1,s+i+1);puts(s+1);
}

猜你喜欢

转载自blog.csdn.net/Light2Chasers/article/details/80773553