第一题:
给你一个字符串,全部都用大写字母组成,如有按顺序排列的连续子串且子串长度大于等于4的时候那就把这种子串表示为开头的的字符+’-‘+结尾的字符的形式。比如 XYZABCD 最后转换为 XYZA-D
思路:
直接模拟,记录每个子串的起始位置和终止位置
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
void solve(string s)
{
int len = s.length();
int tag = 0;
int ed = -1;
int pos = -1;
vector<char>res;
for (int i = 0; i < len; ++i) {
if (i < len-1 && s[i+1]-s[i] == 1) {
if (pos == -1) {
pos = i;
}
ed = i+1;
}
else {
if (pos != -1 && ed - pos + 1 < 4) {
if (ed - pos + 1 > 0)
for (int j = pos; j <= ed; ++j)
res.push_back(s[j]);
}
else if (ed-pos+1 >= 4){
res.push_back(s[pos]);
res.push_back('-');
res.push_back(s[ed]);
}
else
res.push_back(s[i]);
pos = -1;
ed = -1;
}
}
for (int i = 0; i < (int)res.size(); ++i)
cout << res[i];
cout << endl;
}
int main()
{
int n;
string s;
cin >> n;
while(n--) {
cin >> s;
solve(s);
}
return 0;
}
第二题
把一个数N(十进制)的X进制和Y进制写到一起了,形成了一个字符串Z,现在给定X,Y,Z,问这个数N是多少
思路:
用一个mid指针暴力从字符串的中间枚举,前面的部分看做是X进制的数,后面部分看做是Y进制的数,然后转换成十进制之后比较两部分的大小,然后左移或者右移mid指针,直至两个数相等,(我提交的时候只过了40的数据,后面想到中间过程可能会爆int 或许会卡这里)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
map<char,int>mp;
string s;
int len;
void init()
{
for (int i = 0; i < 16; ++i) {
if (i < 10) {
mp[i+'0'] = i;
}
else {
mp['A'+i-10] = i;
}
}
}
long long cal(double a,double b, int flag,int mid)
{
long long r1 = 0;
//cout << mid << endl;
for (int i = mid; i >= 0; --i) {
r1 += mp[s[i]]*1.0 * (int)pow(a,mid-i);
//cout <<s[i] << " " <<mp[s[i]] << " " <<(int)pow(a,mid-i) << endl;
}
//cout << r1 << endl;
long long r2 = 0;
for (int i = len-1; i > mid; --i) {
r2 += mp[s[i]]*1.0 * (long long) pow(b,len-1-i);
}
if (flag == 1) return r1;
else
return r2;
}
long long solve(double a,double b,string s)
{
int mid = (len-1) / 2;
long long r1 = cal(a,b,1,mid);
long long r2 = cal(a,b,0,mid);
//cout << r2 << endl;
while(1) {
if (r1 < r2) {
++mid;
r1 = cal(a,b,1,mid);
r2 = cal(a,b,0,mid);
}
else if (r1 > r2){
--mid;
r2 = cal(a,b,0,mid);
r1 = cal(a,b,1,mid);
}
else if (r1 == r2) return r1;
}
//cout << r1 << endl;
}
int main()
{
int t;
cin >> t;
double a,b;
//string s;
init();
while(t--) {
cin >> a >> b >> s;
len = s.length();
cout << solve(a,b,s) << endl;
}
return 0;
}
/*
3
5 2 113221101000101
13 7 1016
4 12 2222248A
*/