给定一个字符串str和Q查询。每个查询包含一对整数 (i1, i2) 和一个字符“ch”。我们需要在指标I1和新的字符“频道” I2,然后告诉我们,如果字符串str是回文或不替换字符。 (0 <= i1, i2 < string_length)
例子:
Input : str = "geeks" Q = 2
query 1: i1 = 3 ,i2 = 0, ch = 'e'
query 2: i1 = 0 ,i2 = 2, ch = 's'
Output : query 1: "NO"
query 2: "NO"
Explanation :
In query 1 : i1 = 3 , i2 = 0 ch = 'e'
After replacing char at index i1, i2
str[3] = 'e', str[0] = 'e'
string become "eeees" which is not
palindrome so output "NO"
In query 2 : i1 = 0 i2 = 2 ch = 's'
After replacing char at index i1 , i2
str[0] = 's', str[2] = 's'
string become "sesks" which is
palindrome so output "NO"
Input : str = "jasonamat" Q = 3
query 1: i1 = 3, i2 = 8 ch = 'j'
query 2: i1 = 2, i2 = 6 ch = 'n'
query 3: i1 = 3, i2 = 7 ch = 'a'
Output :
query 1: "NO"
query 2: "NO"
query 3: "YES"
一个简单的办法是,对于每个查询,我们在指标替代字符(I1和I2)有一个新的字符“CH”,然后检查是否字符串是回文与否。
以下是上述想法的实现
C++
// C++ program to find if string becomes palindrome
// after every query.
#include
using namespace std;
// Function to check if string is Palindrome or Not
bool IsPalindrome(string &str)
{
int n = strlen(str);
for (int i = 0; i < n/2 ; i++)
if (str[i] != str[n-1-i])
return false;
return true;
}
// Takes two inputs for Q queries. For every query, it
// prints Yes if string becomes palindrome and No if not.
void Query(string &str, int Q)
{
int i1, i2;
char ch;
// Process all queries one by one
for (int q = 1 ; q <= Q ; q++ )
{
cin >> i1 >> i2 >> ch;
// query 1: i1 = 3 ,i2 = 0, ch = 'e'
// query 2: i1 = 0 ,i2 = 2 , ch = 's'
// replace character at index i1 & i2 with new 'ch'
str[i1] = str[i2] = ch;
// check string is palindrome or not
(isPalindrome(str)== true) ? cout << "YES" << endl :
cout << "NO" << endl;
}
}
// Driver program
int main()
{
char str[] = "geeks";
int Q = 2;
Query(str, Q);
return 0;
}
Python3
# Python3 program to find if
# string becomes palindrome
# after every query.
# Function to check if string
# is Palindrome or Not
def isPalindrome(string: list) -> bool:
n = len(string)
for i in range(n // 2):
if string[i] != string[n - 1 - i]:
return False
return True
# Takes two inputs for Q queries.
# For every query, it prints Yes
# if string becomes palindrome
# and No if not.
def Query(string: list, Q: int) -> None:
# Process all queries one by one
for i in range(Q):
# To get space separated
# input from user
inp = list(input().split())
# parsing user inputs as integers
# and strings/char
i1 = int(inp[0])
i2 = int(inp[1])
ch = inp[2]
# query 1: i1 = 3 ,i2 = 0, ch = 'e'
# query 2: i1 = 0 ,i2 = 2 , ch = 's'
# replace character at index
# i1 & i2 with new 'ch'
string[i1] = string[i2] = ch
# check string is palindrome or not
if isPalindrome(string):
print("Yes")
else:
print("No")
# Driver Code
if __name__ == "__main__":
string = list("geeks")
Q = 2
Query(string, Q)
# This code is contributed by
# sanjeev2552
CPP
// C++/c program check if given string is palindrome
// or not after every query
#include
using namespace std;
// This function makes sure that set S contains
// unequal characters from first half. This is called
// for every character.
void addRemoveUnequal(string &str, int index, int n,
unordered_set &S)
{
// If character becomes equal after query
if (str[index] == str[n-1-index])
{
// Remove the current index from set if it
// is present
auto it = S.find(index);
if (it != S.end())
S.erase(it) ;
}
// If not equal after query, insert it into set
else
S.insert(index);
}
// Takes two inputs for Q queries. For every query, it
// prints Yes if string becomes palindrome and No if not.
void Query(string &str, int Q)
{
int n = str.length();
// create an empty set that store indexes of
// unequal location in palindrome
unordered_set S;
// we store indexes that are unequal in palindrome
// traverse only first half of string
for (int i=0; i> i1 >> i2 >> ch;
// Replace characters at indexes i1 & i2 with
// new char 'ch'
str[i1] = str [i2] = ch;
// If i1 and/or i2 greater than n/2
// then convert into first half index
if (i1 > n/2)
i1 = n- 1 -i1;
if (i2 > n/2)
i2 = n -1 - i2;
// call addRemoveUnequal function to insert and remove
// unequal indexes
addRemoveUnequal(str, i1 , n, S );
addRemoveUnequal(str, i2 , n, S );
// if set is not empty then string is not palindrome
S.empty()? cout << "YES\n" : cout << "NO\n";
}
}
// Driver program
int main()
{
string str = "geeks";
int Q = 2 ;
Query(str, Q);
return 0;
}
输入:
3 0 e
0 2 s
输出:
"NO"
"YES"
时间复杂度 O(Q*n)(n 是字符串的长度)
一个有效的解决方案是使用散列。我们创建了一个空的哈希集,用于存储回文中不相等的索引(注意:“我们必须仅存储字符串的前半部分不相等的索引”)。
Given string "str" and length 'n'.
Create an empty set S and store unequal indexes in first half.
Do following for each query :
1. First replace character at indexes i1 & i2 with
new char "ch"
2. If i1 and/or i2 are/is greater than n/2 then convert
into first half index(es)
3. In this step we make sure that S contains maintains
unequal indexes of first half.
a) If str[i1] == str [n - 1 - i1] means i1 becomes
equal after replacement, remove it from S (if present)
Else add i1 to S
b) Repeat step a) for i2 (replace i1 with i2)
4. If S is empty then string is palindrome else NOT
下面是上述想法的 C++ 实现
CPP
// C++/c program check if given string is palindrome
// or not after every query
#include
using namespace std;
// This function makes sure that set S contains
// unequal characters from first half. This is called
// for every character.
void addRemoveUnequal(string &str, int index, int n,
unordered_set &S)
{
// If character becomes equal after query
if (str[index] == str[n-1-index])
{
// Remove the current index from set if it
// is present
auto it = S.find(index);
if (it != S.end())
S.erase(it) ;
}
// If not equal after query, insert it into set
else
S.insert(index);
}
// Takes two inputs for Q queries. For every query, it
// prints Yes if string becomes palindrome and No if not.
void Query(string &str, int Q)
{
int n = str.length();
// create an empty set that store indexes of
// unequal location in palindrome
unordered_set S;
// we store indexes that are unequal in palindrome
// traverse only first half of string
for (int i=0; i> i1 >> i2 >> ch;
// Replace characters at indexes i1 & i2 with
// new char 'ch'
str[i1] = str [i2] = ch;
// If i1 and/or i2 greater than n/2
// then convert into first half index
if (i1 > n/2)
i1 = n- 1 -i1;
if (i2 > n/2)
i2 = n -1 - i2;
// call addRemoveUnequal function to insert and remove
// unequal indexes
addRemoveUnequal(str, i1 , n, S );
addRemoveUnequal(str, i2 , n, S );
// if set is not empty then string is not palindrome
S.empty()? cout << "YES\n" : cout << "NO\n";
}
}
// Driver program
int main()
{
string str = "geeks";
int Q = 2 ;
Query(str, Q);
return 0;
}
输入:
3 0 e
0 2 s
输出:
"NO"
"YES"
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。