//超时代码1号:暴力删除~
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
int f = 0;
while (t--)
{
int ans = 0;
string v1;
string s;
cin >> s;
string v;
for (int i=0; i<s.size(); i++)
{
for (int j=i; j<s.size(); j++)
{
v1.push_back(s[j]);
v = v1;
for (int k=0; k<v.size(); k++)
{
for (int j=k+1; j<v.size(); j++)
{
if (v[k] == v[j])
{
v.erase(k, 1);
j--;
v.erase(j, 1);
}
}
if (v.size()<=1)
{
ans++;
}
}
while (!(v.empty()))
{
int i = 0;
v.erase(i);
}
}
while (!(v1.empty()))
{
int i = 0;
v1.erase(i);
}
}
cout << "Case "<< ++f << ":" << ans << endl;
}
return 0;
}
超时代码2号:
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
string s;
string s1, s2;
int k;
int f = 0;
while (n--)
{
int ans = 0;
cin >> s;
for (int i=0; i<s.size(); i++)
{
for (int j=i; j<s.size(); j++)
{
s1 = s.substr(i, j-i+1);
for (int p = 0; p<s1.size(); p++)
{
k = s1.rfind(s1[p]);
if (k != p && s1[k] == s1[p])
{
s1.erase(k, 1);
s1.erase(p, 1);
p--;
}
}
if (s1.size()<=1)
{
ans++;
}
}
}
cout << "Case " << ++f << ": " << ans << endl;
}
return 0;
}
/*
4
acmicpc
aaaaa
isyoursolutionfastenough
abbabababbaba
*/
超时代码3号:中间往两边扩展扫描~
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
string s;
map<char, int> m;
int ju = 0;
int ans = 0;
int f = 0;
map<char, int> :: iterator it;
while (n--)
{
ans = 0;
cin >> s;
for (int i=0; i<s.size(); i++)
{
for (int j=0; i-j>=0 && i+j<s.size(); j++)
{
m[s[i-j]]++;
if (i-j != i+j)
{
m[s[i+j]]++;
}
ju = 0;
for (it=m.begin(); it!=m.end(); it++)
{
if (it->second%2)
{
ju++;
}
}
if (ju <= 1)
{
ans++;
}
/*for (it=m.begin(); it!=m.end(); it++)
{
cout << it->first <<" ";
}
cout << endl;*/
}
//cout << "1: " << ans << endl;
m.clear();
for (int j=0; i-j>=0 && i+j+1<s.size(); j++)
{
m[s[i-j]]++;
m[s[i+j+1]]++;
ju = 0;
for (it=m.begin(); it!=m.end(); it++)
{
if (it->second%2)
{
ju++;
}
}
if (ju <= 1)
{
ans++;
}
/*for (it=m.begin(); it!=m.end(); it++)
{
cout << it->first <<" ";
}
cout << endl;*/
}
//cout << "2: " << ans << endl;
m.clear();
}
cout << "Case " << ++f << ": " << ans << endl;
}
return 0;
}
/*
isyoursolutionfastenough
abbabababbaba
*/
AC代码:没什么特别的 ,就是简洁大方不超时~
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
using namespace std;
map<char, int> m;
string s;
int main()
{
int n;
scanf("%d", &n);
int ans = 0;
int f = 0;
int cala;
while (n--)
{
ans = 0;
cin >> s;
for (int i=0; i<s.size(); i++)
{
m.clear();
m[s[i]]++;
cala = 1;
for (int j=i+1; j<s.size(); j++)
{
m[s[j]]++;
if (m[s[j]]%2)
{
cala++;
}
else
{
cala--;
}
if (cala <= 1)
{
ans++;
}
}
}
cout << "Case " << ++f << ": " << ans + s.size() << endl;
}
return 0;
}
这是道水题好么,然而刷了一天好么,真是醉了醒, 醒了醉~唔~