标签:要求 code har 允许 res min strlen limit ike
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6244 Accepted Submission(s): 2539
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 int m; 10 char str[1050]; 11 while (cin >> str >> m) 12 { 13 int len = strlen(str); 14 if (len <= m) 15 { 16 cout << "0" << endl; 17 continue; 18 } 19 20 int flag = 0; 21 int n = len; 22 int l = 0, r = m; 23 for (int i = 0; i < n - m; i++) 24 { 25 char min = str[l]; 26 int pos = l; 27 for (int j = l; j <= r; j++) 28 { 29 if (str[j]< min) 30 { 31 min = str[j]; 32 pos = j; 33 } 34 } 35 l = pos + 1; 36 r++; 37 if (!flag && min == ‘0‘) 38 continue; 39 cout << min; 40 flag++; 41 } 42 43 if (!flag) 44 cout << 0; 45 cout << endl; 46 } 47 48 return 0; 49 }
用st表的话也差不多是一个思路,我们先用st表处理得到各个区间最小的数的下标,然后同样按照贪心去求0~m,pos+1~m+1,pos+1~m+2.......
1 #include <iostream> 2 #include <cmath> 3 #include <string> 4 5 using namespace std; 6 7 string s; 8 int n; 9 int st[1050][20]; 10 char ans[1050]; 11 12 int Min(int a, int b) 13 { 14 return s[a] <= s[b] ? a : b; 15 } 16 17 void init() 18 { 19 for (int i = 0; i < s.size(); i++) 20 st[i][0] = i; 21 22 for (int j = 1; (1 << j) < s.size(); j++) 23 for (int i = 0; i + (1 << j) - 1 < s.size(); i++) 24 { 25 st[i][j] = Min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); 26 //cout << i << ends << j << endl; 27 } 28 } 29 30 int search(int l, int r) 31 { 32 int k = (int)(log((double)(r - l + 1)) / log(2.0)); 33 return Min(st[l][k], st[r - (1 << k) + 1][k]); 34 } 35 36 int main() 37 { 38 ios::sync_with_stdio(false); 39 while (cin >> s >> n) 40 { 41 init(); 42 int len = s.size() - n; 43 int pos = 0; 44 int flag = 0; 45 while (len--) 46 { 47 pos = search(pos,n + flag); 48 ans[flag++] = s[pos]; 49 //cout << s[pos] << endl; 50 pos++; 51 } 52 53 flag = 0; 54 55 for (int i = 0; i < s.size() - n; i++) 56 { 57 if (!flag && ans[i] == ‘0‘) 58 continue; 59 flag++; 60 cout << ans[i]; 61 } 62 if (!flag) 63 cout << 0; 64 cout << endl; 65 } 66 67 return 0; 68 }
标签:要求 code har 允许 res min strlen limit ike
原文地址:https://www.cnblogs.com/qq965921539/p/9609015.html