对于输入一个字符串中最后一个只出现一次的字符,若全部字符均有重复,则返回最后一个重复次数最多的字符。
#include<iostream>
#include<string>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
typedef struct
{
char data[255];
int top;
}SqStack;
string rollback(const char *str)
{
SqStack *s = new SqStack;
string ss;
s->top = -1;
for (int i = 0; i < strlen(str); i++)
{
s->top++;
s->data[s->top] = str[i];
}
for (int i = 0; i < strlen(str); i++)
{
ss.push_back(s->data[s->top]);
s->top--;
}
return ss;
}
char find(const char *str)
{
int coun = 0;
int max = 0;
string ss= rollback(str);
for (int i = 0; i < ss.length(); i++)
{
coun = count(ss.begin(), ss.end(), ss[i]);
if (1 == coun)
{
return ss[i];
}
else
max = max>coun ? max : coun;
}
for (int i = 0; i < ss.length(); i++)
{
coun = count(ss.begin(), ss.end(), ss[i]);
if (max == coun)
{
return ss[i];
}
}
}
int main(void)
char a;
a = find(str);
cout << a << endl;;
while (1);
return 0;
}