文章目录
以下题目出自 该网址
Problem A. Super-palindrome——思维
给定一个字符串,输出经过几步可以变成超级回文串
超级回文串定义:对于每一个奇数长度的子串,都是回文串
对于一个奇数n,长度为n的字符串是回文串,那么长度为n+2的字符串想要是回文串,就必须使奇数位上的数相同,偶数位上的数相同,得出结论:
符合超级回文串的字符串一定是 ababababab… 型的回文串,所以只要找到奇数/偶数位上的出现次数最多的字符串就行了
#include <iostream>
#include <string.h>
using namespace std;
int st[130];
int main()
{
int t;
cin >> t;
while(t -- )
{
string a;
cin >> a;
int ans = 0;
int mx = 0;
memset(st, 0, sizeof(st));
for(int i = 0; i < a.size(); i +=2 )
{
st[a[i]] ++ ;
mx = max(st[a[i]], mx);
}
ans += (a.size() + 1) / 2 - mx;
mx = 0;
memset(st, 0, sizeof(st));
for(int i = 1; i < a.size(); i +=2 )
{
st[a[i]] ++ ;
mx = max(st[a[i]], mx);
}
ans += a.size() / 2 - mx;
cout << ans << endl;
}
}
Problem J. Master of GCD——差分+快速幂
一开始以为是线段树,但是嫌代码长细看了一会题,发现差分就可以实现题目要求,但是要用加减法模拟乘法,不然会爆longlong,题目中说了,只有2 和 3 ,所以开俩数组,一个存2,一个存3,然后将差分还原,最后算的时候只需要找出现次数最小的2和3就行了,然后求2和3的快速幂相乘就行了,记得取模,记得加速cin或者直接用scanf
#include <iostream>
#include <string.h>
using namespace std;
const int N = 1e5