题目来源:http://t.csdnimg.cn/4PuEf
题目A 加密攻击
主要是完成交换,就想到swap,然后就用字符做了
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
vector<char> pw;
int char2int(vector<char> pw) {
int res;
string str;
for(int i = 0; i < pw.size(); i++) str += pw[i];
res = stoi(str);
return res;
}
int GCD(int a, int b) {
if(b == 0) return a;
else return GCD(b,a%b);
}
int main() {
int t;
string s;
cin >> t;
while(t--) {
cin >> s;
for(int i = 0; i < s.length(); i++) {
pw.push_back(s[i]);
}
int res = 0;
int a = stoi(s);
for(int i = 0; i < pw.size(); i++) {
for(int j = 0; j < pw.size(); j++) {
swap(pw[i], pw[j]);
int b = char2int(pw);
res = max(res,a*b/GCD(a,b));
swap(pw[i],pw[j]);
}
}
cout << res << endl;
pw.clear();
}
}
题目B 长度最小
首先想到滑动窗口,然后想到标记字符,第一版代码如下
(测试之后发现不是最小的,只能是前边出现的,但是没看明白是哪里的问题,蹲解答)
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
vector<bool> visited(26, false);
bool isFull(vector<bool> visited) {
int cnt = 0;
for(int i = 0; i < visited.size(); i++) {
if(visited[i]) cnt++;
}
if(cnt == 26) return true;
else return false;
}
int main() {
int t;
cin >> t;
while(t--) {
string s;
cin >> s;
int n = s.length();
int ml = INT_MAX;
int start = 0;
int end = 0;
while(end < n) {
visited[s[end]-'a'] = true;
end++;
while(isFull(visited)) {
ml = min(ml, end-start);
visited[s[start]-'a'] = false;
start++;
}
}
if(ml != INT_MAX) cout << ml << endl;
else cout << "-1" << endl;
}
}
第二版用map,测试感觉没什么问题
#include <iostream>
#include <unordered_map>
#include <cstring>
#include <algorithm>
using namespace std;
unordered_map <char,int> window;
//判断a-z是不是都出现了
bool isValid(unordered_map <char,int> window) {
return window.size() == 26;
}
int main() {
int t;
cin >> t;
while(t--) {
string s;
cin >> s;
int n = s.length();
int ml = INT_MAX; //最大化
int start = 0; //窗口起点
int end = 0; //窗口终点
while(end < n) { //end开始往后遍历,更改map中的值
window[s[end]]++;
end++;
//当满足包含a-z时,开始缩小窗口
while(isValid(window)) {
ml = min(ml, end-start); //最小值
window[s[start]]--; //start向后移动,--
if(window[s[start]] == 0) window.erase(s[start]); //滑出窗口
start++;
}
}
//输出
if(ml != INT_MAX) cout << ml << endl;
else cout << "-1" << endl;
}
}
(待更新……