

Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#pragma GCC optimize("03")
#include<bits/stdc++.h>
#define nmax 10000007
#define nmax2 1000006
#define nmax3 100005
#define mod 1000000007
#define fi first
#define se second
#define ma -1e18
#define mi 1e18
#define ll long long
using namespace std;
void start()
{
freopen("BAI3.inp","r",stdin);
freopen("BAI3.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
}
ll tim(string str) {
map<char, int> dem;
vector<bool> a(str.size(), true);
for (int i = 0; i < str.size(); i++) {
dem[str[i]]++;
}
for (int i = 0; i < str.size(); i++) {
if (dem[str[i]] > 1) {
a[i] = false;
}
}
for (int i = 0; i < str.size(); i++) {
if (a[i]) {
return i + 1;
}
}
return -1;
}
int main() {
start();
ll t;
cin >> t;
for (int i = 0; i < t; i++) {
string s;
cin >> s;
cout << tim(s) <<"\n";
}
}
Hãy giúp mọi người biết câu trả lời này thế nào?
![]()
#include <bits/stdc++.h>
using namespace std;
int solve(string s)
{
unordered_map<char,int> cnt;
for(char c:s) cnt[c]++;
for(char c:s) if(cnt[c]==1) return s.find(c)+1;
return -1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
cout<<solve(s)<<endl;
}
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin