题解 | #找位置# 暴力法
找位置
https://www.nowcoder.com/practice/e3b2cc44aa9b4851bdca89dd79c53150
#include <iostream>
#include<string>
#include<vector>
using namespace std;
bool findinrepeat(char a,vector<char> b) //目标字符是否在重复序列中出现,重复序列中是目标字符串中重复的字符,比如字符串“lifeisshit”,重复序列就应该是“is”
{
if (!b.empty())
{
for (int i = 0; b[i] != '\0'; i++)
{
if (a == b[i])
{
return true;//找到了
}
}
}
return false;//没找到
}
int main() {
string a;
while (cin >> a)
{ // 注意 while 处理多个 case
vector<char> repeat; //装入重复序列
for (int i = 0; a[i] != '\0'; i++)
{
if (!(findinrepeat(a[i], repeat))) //重复序列中没有,看看是否加进
{
for (int j = i + 1; a[j] != '\0'; j++)
{
if (a[i] == a[j])
{
repeat.push_back(a[i]); //加入重复序列
break;//终止当前循环,下一个i
}
}
}
}//寻找重复的字符有哪些完毕,全部存入repeat中
//接下来的代码是依照格式打印
for (int i = 0; i < repeat.size(); i++)
{
int counter = 0;
for (int k = 0; a[k] != '\0'; k++)
{
if(a[k] == repeat[i])
{
counter++; //用于检验输出格式中是否带逗号
if (counter > 1)
{
cout << ",";
}
cout << a[k] << ":" <<k;
}
}
cout << endl;
}
}
}
// 64 位输出请用 printf("%lld")
查看8道真题和解析