- 每一個重復的數都只需要加最大的數就可以變成獨一無二的,只需要找出重復的數有幾個即可
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
map<int,int>mp;
int ans=0;
for(int i=1;i<=n;i++){
int x;
cin>>x;
mp[x]++;
}
for(auto [u,v]:mp[x]){
ans+=v-1;
}
cout<<ans;
}
- 枚舉字符串的插入位置和數字0-9,算出左右兩邊的中綴表達式,判斷是否相等即可,技巧是用一個操作數棧,一個值棧模擬,當遇到乘號時,取棧頂兩個數相乘,再插回去,加號不管,最后把棧中值相加就可以求出答案,解決乘法先算的問題。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
bool check(string s)
{
// cout << s << endl;
vector<char> p, q;
int f = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '=')
{
f = 1;
continue;
}
if (!f)
p.push_back(s[i]);
else
q.push_back(s[i]);
}
stack<int> vp, vq;
stack<char> op, oq;
int r1 = 0, r2 = 0;
for (int i = 0; i < p.size(); i++)
{
if (p[i] >= '0' && p[i] <= '9')
{
int j = i;
int tmp = p[j] - '0';
while (j + 1 < p.size() && p[j + 1] >= '0' && p[j + 1] <= '9')
{
j++;
tmp = tmp * 10 + (p[j] - '0');
}
i = j;
vp.push(tmp);
if (op.size() && op.top() == '*')
{
op.pop();
int x1 = vp.top();
vp.pop();
int x2 = vp.top();
vp.pop();
vp.push(x1 * x2);
}
}
else
{
op.push(p[i]);
}
}
while (vp.size())
{
r1 += vp.top();
vp.pop();
}
for (int i = 0; i < q.size(); i++)
{
if (q[i] >= '0' && q[i] <= '9')
{
int j = i;
int tmp = q[j] - '0';
while (j + 1 < q.size() && q[j + 1] >= '0' && q[j + 1] <= '9')
{
j++;
tmp = tmp * 10 + (q[j] - '0');
}
i = j;
vq.push(tmp);
if (oq.size() && oq.top() == '*')
{
// cout << tmp << " " << oq.top() << endl;
oq.pop();
int x1 = vq.top();
vq.pop();
int x2 = vq.top();
vq.pop();
vq.push(x1 * x2);
}
}
else
{
oq.push(q[i]);
}
}
while (vq.size())
{
r2 += vq.top();
vq.pop();
}
// cout << r1 << " " << r2 << endl;
return r1 == r2;
}
bool solve(string &s)
{
for (char x = '0'; x <= '9'; x++)
{
string str = x + s;
// cout<<str<<endl;
if (check(str))
return 1;
}
for (int i = 0; i < s.size(); i++)
{
for (char j = '0'; j <= '9'; j++)
{
string str = s.substr(0, i + 1) + j + s.substr(i + 1);
// cout<<str<<endl;
if (check(str))
return 1;
}
}
return 0;
}
signed main()
{
int t;
cin >> t;
while (t--)
{
string s;
cin >> s;
if (solve(s))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}