重新排列給定的字串,使所有質數倍數指標具有相同字元
字串是一系列字元、數字、字母數字字元和特殊字元。字串的長度是其中包含的字元數。質數是隻能被 1 和自身整除的數。
在這篇文章中,我們給出了一個長度為 n 的示例輸入字串。我們將開發一個程式碼,其思想是在任意位置 p 替換相同的字元,以便從 1 到 n/p 範圍內的字元位置重合。
下面是一些說明問題陳述的示例 −
示例示例
示例 1 - alphaaa
輸出 - haaalap
解釋 - 質數索引 2、4、6 和索引 3、6 包含相同的字元“a”。
示例 1 - alphaa
輸出 - -1
解釋 - 只要我們從字串中減少一個字元“a”,所有必需的
位置不能容納相同的字元。因此,返回的輸出是 -1。
演算法
步驟 1 − 初始化一個對映來記錄字串中出現的字元數。
步驟 2 − 地圖包含一個型別元組
用於儲存字元及其各自的頻率。 步驟 3 − 每次遇到一個字元時,它的頻率都會增加 1。
步驟 4 − 向量 vec 也被初始化以保持反向形式的地圖條目,性質如下:
其中將計數和與其關聯的相應字元推入向量。 步驟 5 − 然後對向量進行排序,以便按其頻率對字元進行排序。
步驟 6 − 位置陣列 pos 宣告為與可能的最大值等效的大小。
步驟 7 − 使用 C++ STL 中提供的 fill() 方法對其初始化為值 1。
步驟 8 − 使用指標 i,從整數值 2 開始啟動字串的迭代,如果此位置的 pos 陣列包含一個值,那麼使用指標 j 評估所有素數倍數。
步驟 9 − 對於所有有效位置,填充有相同字元的位置的計數器都會遞增。
步驟 10 − 否則,評估 i*j 索引的 pos 值。
步驟 11 − 然後儲存出現次數最多的字元的頻率,把它認為是很多。
步驟 12 − 如果佔用的位置數大於出現最多的字元頻率,則返回 -1,因為這是一個不可能的操作。
步驟 13 − 否則,最初所有素數倍數索引(即理想位置)都由出現次數最多的字元填出。
步驟 14 − 然後填充剩餘位置。
步驟 15 − 每次從向量中取出一個字元並填充在剩餘位置。
步驟 16 − 此字元的頻率遞減。
步驟 17 − 當某個特定字元的所有出現都被用完時,則從向量中獲取下一個字元。
步驟 18 − 最後交換字元後列印輸出陣列字串。
示例
以下 C++ 程式碼片段採用一個示例輸入字串,然後將相同字元(如果可能)放入所有素數倍數索引中。
//including the required libraries
#include <bits/stdc++.h>
using namespace std;
//rearranging prime indexes to store same character
void primeindx(string str){
// To store the result after transformation
char res[100005];
int pos[100005];
//calculating the size of string
int len = str.size();
//counter of same elements
int cnt = 0;
//maintaining map to store the number of occurrences of characters in string
map<char, int> map;
for (auto ch : str){
//incrementing count of character
map[ch]+=1;
}
//maintaining a vector to store the different characters in map and their respective counts to sort them according to the frequency
vector<pair<int, char> > vec;
for (auto entry : map)
vec.push_back({ entry.second, entry.first });
//sorting the vector according to the frequencies
sort(vec.begin(), vec.end());
//fill the positions of the array pos with 1
fill(pos + 1, pos + len + 1, 1);
//storing indices correctly in the pos array
for (int i = 2; i <= len / 2; i++) {
//check if the value at pos[i] is equivalent to 1
if (pos[i]) {
//getting all multiples of i
for (int j = 1; i*j<= len; j++) {
int val = i*j;
if (pos[val])
//incrementing the number of same characters is value is contained in the multiple
cnt++;
//else storing 0 in the pos vector at that position
pos[val] = 0;
}
}
}
//getting the occurrence of the character occurring the maximum number of times
int mch = vec.back().first;
if ( mch < cnt) {
//not possible
cout << -1;
return;
}
for (int i = 2; i <= len; i++) {
if (!pos[i]) {
//storing the character at the prime indexes is the count of the same character satisfies
res[i] = vec.back().second;
}
}
//fetching characters after prime indexes have been filled out
int k = 0;
for (int i = 1; i <= len; i++) {
//if ith index contains 1
if (pos[i]) {
//get the character in the result array
res[i] = vec[k].second;
//decrement the count available for the character that has been substitutes
vec[k].first-=1;
//if there are no more occurrences of that particular character, skip to next one
if (vec[k].first == 0)
k++;
}
}
//printing the final result array
for(int i =1;i<=len;i++){
cout << res[i];
}
}
//calling the main method
int main(){
//declaring a sample string
string str = "codeeee";
//prime multiple indexes arrangement of character
primeindx(str);
return 0;
}
輸出
ceeedeo
結論
字元計數是 C++ 字串的一個重要方面。基於字串中可用的字元頻率,可以做出大量決策。對映是儲存資料對的一種有效方式,例如字元及其各自的計數,並且非常直觀。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP