
Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.


#include<bits/stdc++.h>
using namespace std;
int main(){
long long i,dem[1000],d;
string s;
cin >>s;
for(i=0;i<s.size();i++)
dem[s[i]]++;
d=0;
for(i=97;i<=122;i++)
if(dem[i]%2==1)
d++;
if(d>0)
cout<<d-1;
else
cout<<0;
return 0;
}

Đếm số lần xuất hiện của các kí tự 'a'..'z' trong xâu S. Dễ thấy với các kí tự có số lần xuất hiện là chẵn, ta có thể xếp một nửa kí tự đó sang 2 bên thỏa mãn tính chất đối xứng. Đối với kí tự có số lần xuất hiện là lẻ, ta làm tương tự và đưa kí tự còn lại vào trung tâm. Như vậy, để xâu sau khi xóa đối xứng thì chỉ có duy nhất một kí tự có số lần xuất hiện lẻ nên ta sẽ xóa các kí tự lẻ đó, chỉ để lại 1 hoặc 0 cái cuối cùng.
code tham khảo:
int main()
{
string s;
cin >> s;
vector<int> fre(256, 0);
for (char c : s) fre[c]++;
int res = 0;
for (int x : fre) res += (x % 2 == 1);
cout << res - (res > 0);
return 0;
}

Dưới đây là một ví dụ về cách giải quyết bài toán này bằng ngôn ngữ Pascal:
function isPalindrome(s: string): boolean; var i, n: integer; begin n := Length(s); for i := 1 to n div 2 do begin if s[i] <> s[n - i + 1] then begin Result := false; Exit; end; end; Result := true; end; function countSuperPalindromes(s: string): integer; var i, j, n: integer; subStr: string; begin n := Length(s); Result := 0; // Đếm số xâu con đối xứng for i := 1 to n do begin subStr := ''; for j := i to n do begin subStr := subStr + s[j]; if isPalindrome(subStr) then Inc(Result); end; end; // Đếm số xâu con siêu đối xứng for i := 1 to n - 1 do begin subStr := ''; for j := i to n do begin subStr := subStr + s[j]; if isPalindrome(subStr) then Inc(Result); end; end; end; var s: string; begin s := 'ababcb'; writeln(countSuperPalindromes(s)); end.Kết quả của ví dụ trên sẽ là 3, tương ứng với 3 xâu con siêu đối xứng của xâu "ababcb" là "aba", "bcb", và "ababcb".
Lưu ý rằng đây chỉ là một cách giải quyết bài toán và có thể tồn tại các cách giải khác.

def solve():
try:
with open("BAI3.INP", "r") as f_in:
line = f_in.readline().strip().split()
n = int(line[0])
m = int(line[1])
if not (1 < n <= 100 and 1 < m <= 100):
with open("BAI3.OUT", "w") as f_out:
f_out.write("-1")
return
matrix = []
for _ in range(n):
row = list(map(int, f_in.readline().strip().split()))
matrix.append(row)
with open("BAI3.OUT", "w") as f_out:
for i in range(n):
max_val = float('-inf')
positions = []
for j in range(m):
if matrix[i][j] > max_val:
max_val = matrix[i][j]
positions = [i * m + j + 1]
elif matrix[i][j] == max_val:
positions.append(i * m + j + 1)
f_out.write(f"{max_val} {' '.join(map(str, positions))}\n")
except FileNotFoundError:
with open("BAI3.OUT", "w") as f_out:
f_out.write("-1")
except ValueError:
with open("BAI3.OUT", "w") as f_out:
f_out.write("-1")
solve()
Giải thích: - 1. Đọc và kiểm tra dữ liệu đầu vào:
- Đọc \(N\) và \(M\) từ dòng đầu tiên của file BAI3.INP.
- Kiểm tra điều kiện \(1 < N, M \le 100\). Nếu không thỏa mãn, ghi -1 ra file BAI3.OUT và kết thúc.
- Đọc ma trận từ các dòng tiếp theo của file.
- 2. Xử lý từng hàng:
- Lặp qua từng hàng của ma trận (từ 0 đến \(N-1\)).
- Trong mỗi hàng:
- Khởi tạo
max_val
là giá trị nhỏ nhất có thể (- vô cùng) và một danh sách rỗngpositions
. - Lặp qua các phần tử trong hàng (từ 0 đến \(M-1\)):
- Nếu giá trị hiện tại lớn hơn
max_val
: - Cập nhật max_val với giá trị hiện tại.
- Xóa danh sách positions và thêm vị trí hiện tại vào.
- Nếu giá trị hiện tại bằng
max_val
: - Thêm vị trí hiện tại vào danh sách positions.
- Nếu giá trị hiện tại lớn hơn
- Ghi kết quả:
- Ghi
max_val
và các vị trí trongpositions
ra file BAI3.OUT, cách nhau bởi dấu cách.
- Ghi
- Khởi tạo
- 3. Xử lý lỗi:
- Nếu không tìm thấy file, hoặc dữ liệu không hợp lệ (không phải số nguyên), chương trình sẽ ghi -1 ra file BAI3.OUT.
3 4
1 2 3 4
5 6 7 8
9 10 11 12
File BAI3.OUT sẽ có nội dung: Mã 4 4
8 4
12 4

uses crt;
var s:string;
i,d:integer;
begin
clrscr;
readln(s);
d:=length(s);
for i:=1 to d do
if (s[i]='t') s[i]:='b';
cout<<s;
return 0;
Ở giữa (s[i]='t') s[i]:'b'; có thêm gì k ạ
Sao nó cứ báo lỗi

Program GIAINEN;
Var s,st: string;
j,i,k,l: byte;
begin
readln(s);
while length(s)<>0 do
begin
for i:=1 to length(s) do
if s[i] in ['A'..'Z'] then st:=st+s[i]
else if s[i] in ['1'..'9'] then break;
for i:=1 to length(s) do
if s[i] in ['2'..'9'] then
begin
val(s[i],k);
l:=i;
break;
end;
for j:=1 to k do write(st);
delete(s,1,l);
st:='';
end;
readln
end.

Program HOC24;
var x: integer;
begin
write('Nhap so: '); readln(x);
if x div 1000=1 then write('Mot nghin ');
if x div 1000=2 then write('Hai nghin ');
if x div 1000=3 then write('Ba nghin ');
if x div 1000=4 then write('Bon nghin ');
if x div 1000=5 then write('Nam nghin ');
if x div 1000=6 then write('Sau nghin ');
if x div 1000=7 then write('Bay nghin ');
if x div 1000=8 then write('Tam nghin ');
if x div 1000=9 then write('Chin nghin ');
if x div 1000<>0 then
if x div 100=0 then write('khong tram ');
if x div 100=1 then write('mot tram ');
if x div 100=2 then write('hai tram ');
if x div 100=3 then write('ba tram ');
if x div 100=4 then write('bon tram ');
if x div 100=5 then write('nam tram ');
if x div 100=6 then write('sau tram ');
if x div 100=7 then write('bay tram ');
if x div 100=8 then write('tam tram ');
if x div 100=9 then write('chin tram ');
if (x div 1000<>0) or (x div 100<>0) then
begin
if (x div 10<>0) then
begin
if x div 10=1 then write('muoi ');
if x div 10=2 then write('hai muoi ');
if x div 10=3 then write('ba muoi ');
if x div 10=4 then write('bon muoi ');
if x div 10=5 then write('nam muoi ');
if x div 10=6 then write('sau muoi ');
if x div 10=7 then write('bay muoi ');
if x div 10=8 then write('tam muoi ');
if x div 10=9 then write('chin muoi ');
if x mod 10=1 then write('mot');
if x mod 10=2 then write('hai');
if x mod 10=3 then write('ba');
if x mod 10=4 then write('bon');
if x mod 10=5 then write('lam');
if x mod 10=6 then write('sau');
if x mod 10=7 then write('bay');
if x mod 10=8 then write('tam');
if x mod 10=9 then write('chin');
end else
begin
if x mod 10=1 then write('le mot');
if x mod 10=2 then write('le hai');
if x mod 10=3 then write('le ba');
if x mod 10=4 then write('le tu');
if x mod 10=5 then write('le nam');
if x mod 10=6 then write('le sau');
if x mod 10=7 then write('le bay');
if x mod 10=8 then write('le tam');
if x mod 10=9 then write('le chin');
end;
readln
end.
Program CHUOI_CHU_THUONG;
Uses Crt;
Var i,l:integer;
st:string[50];
Begin
Clrscr;
Writeln('DOI CHUOI SANG CHUOI CHU THUONG');
Writeln('------------------------------------------------------');
Write('Nhap chuoi ky tu: '); Readln(st);
For i:=1 to length(st) do
If (st[i]>='A') and (st[i]<='Z') then
st[i]:= chr(ord(st[i])+32);
Writeln;
Writeln('Chuoi doi thanh chu thuong la : ');
Writeln(st);
Writeln; l:=length(st);
st[l]:=upcase(st[l]);
For i:=l downto 2 do
If st[i]=' ' then st[i-1]:=upcase(st[i-1]);
Writeln('Chuoi cac ky tu cuoi cua tu la ky tu hoa: ');
Writeln(st);
Readln;
End.
Học tốt
Program DOI_CHUOI_CHU_HOA;
Uses Crt;
Var i:integer;st:string;
Begin
Clrscr;
Writeln('DOI CHUOI SANG CHUOI HOA');
Writeln('--------------------------------------');
Write('Nhap ho ten:');readln(st);
st[1]:=upcase(st[1]);
For i:=1 to length(St) do
If st[i]=' ' then st[i+1]:=upcase(st[i+1]);
Writeln('Ho ten sau khi doi lan 1 la: ',st);
For i:=1 to length(St) do
st[i]:=upcase(st[i]);
Writeln('Ho ten sau khi doi lan 2 la: ',st);
Readln;
End.