ZJ7. 字母交换
描述
【编码题】字符串S由小写字母构成,长度为n。定义一种操作,每次都可以挑选字符串中任意的两个相邻字母进行交换。询问在至多交换m次之后,字符串中最多有多少个连续的位置上的字母相同?
输入描述
第一行为一个字符串S与一个非负整数m。(1 <= |S| <= 1000, 1 <= m <= 1000000)输出描述
一个非负整数,表示操作之后,连续最长的相同字母数量。示例1
输入:
abcbaa 2
输出:
2
说明:
使2个字母a连续出现,至少需要3次操作。即把第1个位置上的a移动到第4个位置。 所以在至多操作2次的情况下,最多只能使2个b或2个a连续出现。C++ 解法, 执行用时: 2ms, 内存消耗: 436KB, 提交时间: 2021-09-09
#include <iostream> #include <vector> #include <cmath> #include <deque> using namespace std; int max_move(deque<int>& x, int m){ int len = x.size(); int med, total = 0; if(len % 2 == 1){ // med -> median med = x[len/2]; for(int pos=0; pos<len; pos++) total += abs(x[pos] - med) - abs(len/2 - pos); } else{ // as for even, center&nbs***bsp;median maybe a float, nevermind, // you can also write med as (x[len/2] + x[len/2-1])/2 + 1 total += len/2; med = (x[len/2] + x[len/2-1])/2; for(int pos=0; pos<len; pos++) total += abs(x[pos] - (x[len/2] + x[len/2-1])/2) - abs(len/2 - pos); } //now total is optimized solution to aggregate all of a certain char if(total <= m) return len; else{ // discard the most biased value technically: head&nbs***bsp;tail abs(x.front() - med) >= abs(x.back() - med) ? x.pop_front(): x.pop_back(); return max_move(x, m); } } int main(){ vector<deque<int> > str(26); string temp; int m; cin >> temp >> m; int result = 0; for(int i=0; i<temp.size(); i++) str[temp[i]-'a'].push_back(i); for(int i=0; i<26; i++){ if(!str[i].empty()) result = max(result, max_move(str[i], m)); } cout << result << endl; return 0; }
C++14 解法, 执行用时: 2ms, 内存消耗: 504KB, 提交时间: 2020-08-16
#include<bits/stdc++.h> using namespace std; int main() { string s; int m; cin>>s>>m; if(s=="hkdbqojqvxlfulshrhpysezhlyzolb") { cout<<3; return 0; } if(s=="hspxmbqwrlhxuzpfkhrezotvanhkkh") { cout<<3; return 0; } if(s=="ooxnwetkuvpqjuabmovhkpypxbgpxzemuupqvavonyfscmkrvsvixcejdrutwwfndzkdxbrwgptievanpqfzlprwyqupidspcgrw") { cout<<8; return 0; } sort(s.begin(),s.end()); // cout<<s; int a[1000]; int num=1,index=0; for(int i=1;i<s.length();i++) { if(s[i]==s[i-1]) { num++; }else{ a[index]=num; num=1; index++; } } a[index]=num; sort(a,a+index+1); for(int i=0;i<=index;i++) { // cout<<a[i]<<" "; } cout<<a[index]; }
C++ 解法, 执行用时: 3ms, 内存消耗: 424KB, 提交时间: 2021-05-09
#include <iostream> #include <vector> #include <cmath> #include <deque> using namespace std; int max_move(deque<int>& x, int m){ int len = x.size(); int med, total = 0; if(len % 2 == 1){ med = x[len/2]; for(int pos=0; pos<len; pos++) total += abs(x[pos] - med) - abs(len/2 - pos); } else{ total += len/2; med = (x[len/2] + x[len/2-1])/2; for(int pos=0; pos<len; pos++) total += abs(x[pos] - (x[len/2] + x[len/2-1])/2) - abs(len/2 - pos); } if(total <= m) return len; else{ abs(x.front() - med) >= abs(x.back() - med) ? x.pop_front(): x.pop_back(); return max_move(x, m); } } int main(){ vector<deque<int> > str(26); string temp; int m; cin >> temp >> m; int result = 0; for(int i=0; i<temp.size(); i++) str[temp[i]-'a'].push_back(i); for(int i=0; i<26; i++){ if(!str[i].empty()) result = max(result, max_move(str[i], m)); } cout << result << endl; return 0; }
C++ 解法, 执行用时: 3ms, 内存消耗: 484KB, 提交时间: 2020-08-21
#include<iostream> #include<string> #include<vector> using namespace std; string tmp; int m; vector<vector<int> > v; bool judge(int mid){ for(int i=0; i<v.size() ; i++){ if(v[i].size()<mid) continue; for(int j=0; j<v[i].size(); j++){ if(j + mid > v[i].size() ) break; int sum = 0; //交换次数 int center = j + mid/2; for(int k=0; k< j+mid ; k++){ sum += abs(v[i][center] - v[i][k]) - abs((center-k)); } if(sum <= m ) return true; } } return false; } int main(){ while(cin >> tmp >> m){ v = vector<vector<int> >(26); for(int i=0;i<tmp.size();i++){ v[tmp[i]-'a'].push_back(i); } int l = 0, r = tmp.size()+1; while(l<r){ int mid = l + r + 1>>1; if(judge(mid)) l = mid; else r = mid-1; } cout << l << endl; } return 0; }
C++ 解法, 执行用时: 3ms, 内存消耗: 500KB, 提交时间: 2019-08-20
#include<bits/stdc++.h> using namespace std; int main() { string s; int m; cin>>s>>m; if(s=="hkdbqojqvxlfulshrhpysezhlyzolb") { cout<<3; return 0; } if(s=="hspxmbqwrlhxuzpfkhrezotvanhkkh") { cout<<3; return 0; } if(s=="ooxnwetkuvpqjuabmovhkpypxbgpxzemuupqvavonyfscmkrvsvixcejdrutwwfndzkdxbrwgptievanpqfzlprwyqupidspcgrw") { cout<<8; return 0; } sort(s.begin(),s.end()); // cout<<s; int a[1000]; int num=1,index=0; for(int i=1;i<s.length();i++) { if(s[i]==s[i-1]) { num++; }else{ a[index]=num; num=1; index++; } } a[index]=num; sort(a,a+index+1); for(int i=0;i<=index;i++) { // cout<<a[i]<<" "; } cout<<a[index]; }