菜鸡思路感觉是对的但是只有10分其他都tle了

P3612 [USACO17JAN] Secret Cow Code S

hehe123313 @ 2023-12-23 16:00:00

#include<iostream>

using namespace std;
typedef long long ll;
ll n;
string s;
ll now;
int main()
{
    cin>>s>>n;
    now=s.size();
    while(now*2<n)
        now*=2;
   while(1==1)
   {
       if(n<=s.size())
       {
           cout<<s[n-1];
           return 0;
       }
       if(n==now+1)
       {
           n=n-1;
           now/=2;
           continue;
       }
       else
       {
           n=n-now-1;
           now/=2;
           continue;
       }
   }

    return 0;
}

by hehe123313 @ 2023-12-23 16:00:36

就是找到每次位置变化的规律,然后不断往前面递推


by wangderui111 @ 2024-01-14 17:51:26

#include<bits/stdc++.h>
using namespace std;
string fz(string a){
    string b;
    b+=a[a.length()-1];
    for(long long int i=0;i<a.length()-1;i++){
        b+=a[i];
    }
    return a+b;
}int main(){
    string c;
    cin>>c;
    long long int d;
    cin>>d;
    while(c.length()<d){
        c=fz(c);
    }cout<<c[d-1]<<endl;
    return 0;
}

40分


by cc110711 @ 2024-02-03 16:33:23

我刚刚也是这样 问题在于n可能会比now小 然后执行最后一个else语句的时候n就会变成负数 最后输出s[n-1]但n-1小于0


|