AOJ 10.目标柏林
优化!优化!
读入的字符串包含空格!
数据量较大,vector string都会降低效率
简直优化哭了要( ▼-▼ )
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <string> 6 #include <iostream> 7 #include <vector> 8 using namespace std; 9 10 #define REP(n) for(int o=0;o<n;o++) 11 12 const int maxn = 1005; 13 14 int read(char s[]) { 15 char c; 16 int i = 0; 17 c = getchar(); 18 if(c == EOF) 19 return 0; 20 while(c != '\n'&&c != EOF) { 21 s[i++] = c; 22 c = getchar(); 23 } 24 s[i] = '\0'; 25 return i; 26 } 27 28 int main() { 29 //std::ios::sync_with_stdio(false); 30 char s[maxn]; 31 while(read(s)) { 32 int len = (int)strlen(s); 33 for(int i = len / 2 + 1;i > 0;i--) { 34 char temp[maxn]; 35 strcpy(temp,s); 36 int v[maxn]; 37 int it = 0; 38 for(int j = i;j <= len;j += i) 39 v[it++] = j; 40 41 for(int j = 0;j < it;j++) { 42 int t = (i % 2 ? j + 1 : j - 1); 43 t = t == -1 ? it - 1 : t == it ? t == 0 : t; 44 s[v[j] - 1] = temp[v[t] - 1]; 45 } 46 //cout << " " << s << endl; 47 //v.erase(v.begin(),v.end()); 48 } 49 cout << s << endl; 50 } 51 return 0; 52 }
然而,我并不能保证我说的是对的。请自行验证,如有错误,请指正
新博客地址
https://www.oyohyee.com
https://www.oyohyee.com