- 构建回文串检测
给你一个字符串 s,请你对 s 的子串进行检测。
每次检测,待检子串都可以表示为 queries[i] = [left, right, k]。我们可以 重新排列 子串 s[left], …, s[right],并从中选择 最多 k 项替换成任何小写英文字母。
如果在上述检测过程中,子串可以变成回文形式的字符串,那么检测结果为 true,否则结果为 false。
返回答案数组 answer[],其中 answer[i] 是第 i 个待检子串 queries[i] 的检测结果。
注意:在替换时,子串中的每个字母都必须作为 独立的 项进行计数,也就是说,如果 s[left…right] = “aaa” 且 k = 2,我们只能替换其中的两个字母。(另外,任何检测都不会修改原始字符串 s,可以认为每次检测都是独立的)
示例:
输入:s = “abcda”, queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
输出:[true,false,false,true,true]
解释:
queries[0] : 子串 = “d”,回文。
queries[1] : 子串 = “bc”,不是回文。
queries[2] : 子串 = “abcd”,只替换 1 个字符是变不成回文串的。
queries[3] : 子串 = “abcd”,可以变成回文的 “abba”。 也可以变成 “baab”,先重新排序变成 “bacd”,然后把 “cd” 替换为 “ab”。
queries[4] : 子串 = “abcda”,可以变成回文的 “abcba”。
提示:
1 <= s.length, queries.length <= 10^5
0 <= queries[i][0] <= queries[i][1] < s.length
0 <= queries[i][2] <= s.length
s 中只有小写英文字母
题意:给定字符串Str
,多次询问区间[L,R)
里的字符修改至多K个
后的排列
是否可能组成回文串
tips:
- 回文串
最多有一个奇数
字符, - 因此只需要高效查询字符串区间有多少个奇数字符,是否有足够的操作次数即可
- 字符串不会被修改(静态询问),想到
前缀和
- 开
26N
的空间统计每个字符的前缀和 - 如果有
cnt
个奇数字符,则只需要cnt/2
次操作即可得到可能排成回文串的串
例如,abcde
有5个奇数字符,则修改为aadde(排列后可以构成回文adeda)
2个奇数字符,只需要5/2=2
次操作
#define debug
#ifdef debug
#include <time.h>
#include "win_majiao.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
typedef vector<vector<int> > VVI;
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO{
char print_f[105];
void read() {}
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
typedef vector<int> VI;
int psum[32][MAXN];
class Solution {
public:
void build(string& s) { //对每个字符构建前缀和数组
for(int i=0; s[i]; i++) {
int pos = s[i] - 'a';
for(int k=0; k<27; k++)
psum[k][i+1] = psum[k][i];
psum[pos][i+1] = psum[pos][i] + 1;
}
}
bool query(string& str, int L, int R, int K) {
if(L+1 == R) {
char lef = str[L], rig = (K ? str[L] : str[R]);
return lef == rig;
}
int cnt[32] = { 0 };
int sum = 0;
for(int i=0; i<27; i++) { //统计区间[L,R)内有多少个奇数个数的字符
cnt[i] = psum[i][R] - psum[i][L-1];
sum += (cnt[i] & 1);
}
return (sum/2 <= K);
}
vector<bool> canMakePaliQueries(string s, VVI& q) {
vector<bool> ans(q.size(), false);
build(s);
for(int i=0; i<q.size(); i++) {
int L = q[i][0] + 1, R = q[i][1] + 1, K = q[i][2];
ans[i] = query(s, L, R, K);
}
return ans;
}
};
#ifdef debug
signed main() {
Solution s;
string str = "abcda";
VVI que = {{3,3,0},{1,2,0},{0,3,1},{0,3,2},{0,4,1}};
// VVI que = {{0,3,2}};
vector<bool> ans = s.canMakePaliQueries(str, que);
forvec(ans);
return 0;
}
#endif
java
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
class Solution {
int psum[][] = new int[32][Main.MAXN];
public List<Boolean> canMakePaliQueries(String s, int[][] q) {
List<Boolean> ans = new ArrayList<>();
build(s);
for(int i=0; i<q.length; i++) {
int L = q[i][0]+1, R = q[i][1]+1, K = q[i][2];
ans.add(query(L, R, K));
}
return ans;
}
private void build(String s) {
char[] ch = s.toCharArray();
for(int i=0; i<ch.length; i++) {
for(int k=0; k<27; k++) psum[k][i+1] = psum[k][i];
int pos = ch[i] - 'a';
psum[pos][i+1] = psum[pos][i] + 1;
}
}
boolean query(int L, int R, int K) {
int sum = 0;
for(int i=0; i<27; i++) {
int cnt = (psum[i][R]-psum[i][L-1]);
if((cnt&1) > 0) sum ++;
}
return ((sum/2) <= K);
}
}
public class Main {
public static final boolean debug = true;
public static String INPATH = "C:\\Users\\majiao\\Desktop\\test.txt",
OUTPATH = "C:\\Users\\majiao\\Desktop\\out.txt";
public static StreamTokenizer tok;
public static BufferedReader cin;
public static PrintWriter cout;
public static long start_time = 0, out_time = 0;
public static int n, m, K, Q, MAXN = (int)1e5+7, INF = 0x3f3f3f3f;
public static byte buf[] = new byte[MAXN];
public static void main(String[] args) throws IOException {
main_init();
if(debug) { start_time = System.currentTimeMillis(); }
if(false) { System.setOut(new PrintStream(OUTPATH)); }
Solution solu = new Solution();
// int a[] = { };
int q[][] = {{3,3,0},{1,2,0},{0,3,1},{0,3,2},{0,4,1}};
String s = "abcda";
System.out.println(solu.canMakePaliQueries(s, q));
if(debug) {
out_time = System.currentTimeMillis();
cout.printf("run time : %d ms\n", out_time-start_time);
}
cout.flush();
}
public static void show(List<Object> list, Object... obj) {
cout.printf("%s : ", obj.length>0 ? obj[0] : "");
for(Object x : list) {
cout.printf("[%s] ", x);
}
cout.printf("\n");
}
public static void show(Map<Object, Object> mp, Object... obj) {
cout.printf("%s : ", obj.length>0 ? obj[0] : "");
Set<Map.Entry<Object, Object>> entries = mp.entrySet();
for (Map.Entry<Object, Object> en : entries) {
cout.printf("[%s,%s] ", en.getKey(), en.getValue());
}
cout.printf("\n");
}
public static<T> void forarr(T arr[], int ...args) {
int lef = 0, rig = arr.length - 1;
if(args.length > 0) { lef = args[0]; rig = args[1]; }
cout.printf(" : ");
for( ; lef<=rig; lef++) {
cout.printf("[%s] ", args[lef]);
}
cout.printf("\n");
}
public static void main_init() {
try {
if (debug) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream(INPATH)));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
// cout = new PrintWriter(OUTPATH);
tok = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String next_str() {
try {
tok.nextToken();
if (tok.ttype == StreamTokenizer.TT_EOF)
return null;
else if (tok.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tok.nval);
} else if (tok.ttype == StreamTokenizer.TT_WORD) {
return tok.sval;
} else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static int read_int() {
String tmp_next_str = next_str();
return null==tmp_next_str ? -1 : Integer.parseInt(tmp_next_str);
}
public static long read_long() { return Long.parseLong(next_str()); }
public static double read_double() { return Double.parseDouble(next_str()); }
public static BigInteger read_big() { return new BigInteger(next_str()); }
public static BigDecimal read_dec() { return new BigDecimal(next_str()); }
class Pair implements Comparable<Pair>{
int fst, sec;
public Pair() { }
public Pair(int fst, int sec) {
this.fst = fst;
this.sec = sec;
}
@Override
public int compareTo(Pair o) {
return fst - o.fst == 0 ? sec - o.sec : fst - o.fst;
}
}
}