我正在研究一个类的代码,它要求我在字符串中输出重复项。此字符串可以包含任何ascii字符,但输出只需显示重复的字符及其重复的总次数。
以下是一些示例输入和输出
妈妈,m:2taco,没有重复
干得好,o:3
tacocat,t:2 c:2 a:2
我的代码适用于除最后一个测试用例以外的所有测试用例,t:2和a:2出现两次,现在我得出的结论是我需要在某处存储重复的字符并对该列表运行检查以查看是否该副本已经打印,所以我尝试使用矢量。
我的方法是在打印重复项时将字符推入向量,如果字符已经在向量中,则在打印时跳过该字符。但我还没有找到办法解决这个问题。我尝试使用find()
中的#include<algorithm>
,但遇到了我无法解决的语法错误。有没有我可以申请的功能?或者我是以一种糟糕的方式解决这个问题?
我找到了find()
here&amp;的实施我看了here,但他们不匹配,当我尝试应用它时,它完全破坏了我的代码。
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector <char> alreadyprintedcharacters;
void findrepeats(string const&);
int main()
{
string input;
cout << "Enter the input : ";
getline(cin, input);
findrepeats(input);
return 0;
}
void findrepeats(string const &in)
{
int trackerOfDuplicates = 0;
int asciiArray[256];
char ch;
int charconv;
for (int i = 0; i < 256; i++) // creates my refference array for the comparison and sets all the values equal to zero
asciiArray[i] = 0;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
if (asciiArray[charconv] == 0)
{
asciiArray[charconv] = 1;
}
else if (asciiArray[charconv] > 0)
{
asciiArray[charconv] = asciiArray[charconv]++;
}
}
bool trip = false;
for (unsigned int i = 0; i < in.length(); i++)
{
char static alreadyprinted;
char ch = in[i];
if ((asciiArray[ch] > 1) && (ch != alreadyprinted) && (find(alreadyprintedcharacters.begin(), alreadyprintedcharacters.end(), ch)!= alreadyprintedcharacters.end()))// change reflected HERE
{
cout << in[i] << " : " << asciiArray[ch] << endl;//???? maybe a nested loop
trip = true;
alreadyprinted = ch;
alreadyprintedcharacters.push_back(alreadyprinted);
}
}
if (trip == false)
cout << "No repeated characters were found.\n";
}
答案 0 :(得分:2)
如果您修复了与std::find
相关的错误,那么您的代码可以正常使用(为std::find
提供正确的输出):
bool
不会返回std::vector<char>::iterator
,它会返回一个迭代器(在您的情况下为std::find
)。如果您想检查alreadyprintedcharacters.end()
是否找到了某些内容,则应将其与std::find
进行比较,因为如果uint64_t x = 1000000000ull;
找不到内容,则会int64_t y = 1000000000ll;
返回。
答案 1 :(得分:2)
您可以创建一个256的整数数组,并首先将其初始化为0。然后遍历字符串中的字符并递增与该字母对应的每个索引。最后,您可以打印出值大于1的字母。只需将您的findrepeats函数更改为以下内容:
void findrepeats(string const &in)
{
int asciiArray[256];
char ch;
int charconv;
bool foundAny = false;
for (int i = 0; i < 256; i++) asciiArray[i] = 0;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
asciiArray[charconv]++;
}
for (unsigned int i = 0; i < 256; i++)
{
char static alreadyprinted;
if (asciiArray[i] > 1)
{
foundAny = true;
cout << static_cast<char>(i) << " : " << asciiArray[i] << endl;
}
}
if (!foundAny)
cout << "No repeated characters were found.\n";
}
答案 2 :(得分:1)
您必须在代码中进行以下更改
更改用于比较的参考数组的循环体,并设置所有值,如下所示:
//your code
else if (asciiArray[charconv] > 0)
{
asciiArray[charconv] = asciiArray[charconv]++;
}
在上面的代码中,asciiArray[charconv]
的值不会发生变化,因为它是一个后期增量asciiArray[charconv]++;
,要么将其更改为预增量++asciiArray[charconv];
,要么写{{1} }}
Here is a link to this why it doesn't increment
此外,您可以像这样更改循环,更简化:
asciiArray[charconv] = asciiArray[charconv]+1;
将找到的类型更改为for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
asciiArray[charconv]++;
}
coz std::vector<char>::iterator
将迭代器返回到比较等于val&amp;的范围内的第一个元素。如果没有元素匹配,则该函数最后返回。
find
然后你的情况应该是
std::vector<char>::iterator found = find(alreadyprintedcharacters.begin(), alreadyprintedcharacters.end(), ch);
答案 3 :(得分:1)
我不明白为什么你需要所有的代码(假设你说你不能使用std::map
)。
您声明了一个256的数组并将每个项目设置为0,这是正常的:
for (int i = 0; i < 256; i++)
asciiArray[i] = 0;
现在下一步应该很简单 - 只需遍历字符串,一次一个字符,然后递增数组中的相关值。你似乎是从这种方式开始,然后去做其他事情的切线:
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i]; // ok
asciiArray[ch]++;
如果我们发现刚刚增加的字符数是&gt;,我们可以将布尔设置为true
。 1:
bool dup = false;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i]; // ok
asciiArray[ch]++;
if ( asciiArray[ch] > 1 )
dup = true;
}
这是预处理字符串的整个循环。然后你需要一个循环来打印出结果。
关于打印,只有在有重复的情况下才能浏览数组,并且只需检查dup
值即可知道这一点。如果字符i
处的数组值为&gt; 1,你打印该角色的信息,如果没有,跳到下一个。
我不会显示最后一步的代码,因为这是作业。
答案 4 :(得分:1)
上周刚刚遇到类似的问题,这就是我所做的,也许不是最好的解决方案,但它确实运作良好。
string str("aer08%&#&%$$gfdslh6FAKSFH");
vector<char> check;
vector<int> counter;
//subscript is the bridge between charcheck and count. counter[sbuscript] store the times that check[subscript] appeared
int subscript = 0;
bool charisincheck = false;
for (const auto cstr : str) //read every char in string
{
subscript = 0;
charisincheck = false;
for (const auto ccheck : check) // read every element in charcheck
{
if (cstr == ccheck)//check if the char get from the string had already existed in charcheck
{
charisincheck = true; //if exist, break the for loop
break;
}
subscript++;
}
if (charisincheck == true) //if the char in string, then the count +1
{
counter[subscript] += 1;
}
else //if not, add the new char to check, and also add a counter for this new char
{
check.push_back(cstr);
counter.push_back(1);
}
}
for (decltype(counter.size()) i = 0; i != counter.size(); i++)
{
cout << check[i] << ":" << counter[i] << endl;
}met
答案 5 :(得分:0)
import java.util.*;
class dublicate{
public static void main(String arg[]){
Scanner sc =new Scanner(System.in);
String str=sc.nextLine();
int d[]=new int[256];
int count=0;
for(int i=0;i<256;i++){
d[i]=0;
}
for(int i=0;i<str.length();i++){
if(d[str.charAt(i)]==0)
for(int j=i+1;j<str.length();j++){
if(str.charAt(i)==str.charAt(j)){
d[str.charAt(i)]++;
}
}
}
for(char i=0;i<256;i++){
if(d[i]>0)
System.out.println(i+" :="+(d[i]+1));
}
}
}
答案 6 :(得分:0)
//here simple code for duplicate characters in a string in C++
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main(){
clrscr();
char str[100];
cin>>str;
int d[256];
int count=0;
for(int k=0;k<256;k++){
d[k]=0;
}
for(int i=0;i<strlen(str);i++){
if(d[str[i]]==0)
for(int j=i+1;j<strlen(str);j++){
if(str[i]==str[j]){
d[str[i]]++;
}
}
}
for(int c=0;c<256;c++){
if(d[c]>0)
cout<<(char)c<<" :="<<(d[c]+1)<<"\n";
}
getch();
}