关于字符串的一个算法

zhouhong0801 2007-09-26 07:44:21
在一个字符串中找到第一个只出现一次的字符。

比如abaccdeff,输出b。
...全文
215 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
短歌如风 2007-09-27
  • 打赏
  • 举报
回复
最后一段写错了,修改如下:

如果想减少内容用量,可以改用std::map<char/wchar_t, int>代替,这样所需附加空间可能会减少(也可能会增加,取决于数据中出现的字符的种类),但时间复杂度提升为O( n * log(n) ).

这种方法其实就是Chiyer(星羽)所写的方法。不过Chiyer(星羽)的代码有一个错误:应该遍历str检查在mapStr中获得的计数,而不应该遍历mapStr,否则找到的结果就不是“第一个”只出现一次的字符,而是“最小的一个”只出现一次的字符。而在楼主给的测试数据中,这两个逻辑的结果恰好相等。

短歌如风 2007-09-26
  • 打赏
  • 举报
回复
无论数据有多少,附加空间都是256个整数,明显是O(1)。256 * O(1) 结果还是O(1)。

对于char的串,字符只有256种。事实上,对于C风格字符串,有效字符只有255种。

如果是wchar_t的串,则需要64k个整数的附加空间,虽然这个数字不小,但仍然是O(1)。

如果想减少内容用量,可以改用std::map<string/wstring, int>代替,这样所需附加空间可能会减少(也可能会增加,取决于数据中出现的字符的种类),但时间复杂度提升为N*O(N)。
Michael_Xie 2007-09-26
  • 打赏
  • 举报
回复
to plainsong:
int counts[256];
这算空间复杂度为O(1)吗?你这是以空间换时间的策略。况且字符数不一定只有256种呀!
zhaoyue12365 2007-09-26
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;
int main()
{
char ch=0;
string a;
int i,j,x;
cin>>a;
cout<<(x=a.length());
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(a[i]==a[j])
break;
if(j==(x-1))
ch=1;
}
if(ch==1)
{
cout<<a[i];
break;
}
}
}
也可以,哈哈
zhaoyue12365 2007-09-26
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
int main()
{
char a[50],ch=0;
gets(a);
int i,j,x=strlen(a);
cout<<x;
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(a[i]==a[j])
break;
if(j==(x-1))
ch=1;
}
if(ch==1)
{
cout<<a[i];
break;
}
}
}
经过调试,可以实现
qghktit 2007-09-26
  • 打赏
  • 举报
回复

用find() 即可
短歌如风 2007-09-26
  • 打赏
  • 举报
回复

std::string line;
int counts[256];
getline( std::cin, line );
std::memset( counts, 0, 256 * sizeof(int) );
std::string::iterator i;
for( i = line.begin(); i != line.end(); ++ i)
{
++ counts[(unsigned char)*i];
}
for( i = line.begin(); i != line.end() && counts[(unsigned char)*i] != 1; ++ i )
{
}
if( i != line.end() )
{
std::cout << "\n" << * i << std::endl;
}
else
{
std::cout << "\nNothing found" << std::endl;
}


时间复杂度O(n),空间复杂度O(1)
BenjaminHuang 2007-09-26
  • 打赏
  • 举报
回复
使用 STL 中的 map,利用字串中的字符作为 Key,这样的方式确实比较明了,帮3楼的顶一下
BenjaminHuang 2007-09-26
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

using namespace std;

void FindFirstPartner(char *src);

struct DictEntry
{
char Item;
int Appearance;
};

int main()
{
FindFirstPartner("abbaccdeff");
return 0;
}

void FindFirstPartner(char *src)
{
if(NULL == src || 0 == strcmp(src, ""))
{
cout << "Passing empty string to function FindFirstPartner." << endl;
return;
}

int Len = strlen(src);

DictEntry *Dictionary = new DictEntry[Len];
memset(Dictionary, 0x00, sizeof(DictEntry) * Len);

char Character = 0;
bool FoundInDict = false;
int DictIndex = 0;

for(int i = 0; i < Len; i++)
{
Character = src[i];

for(int j = 0; j < DictIndex; j++)
{
if(Character == Dictionary[j].Item)
{
FoundInDict = true;
Dictionary[j].Appearance++;
break;
}
}

if(false == FoundInDict)
{
Dictionary[DictIndex].Item = Character;
Dictionary[DictIndex].Appearance = 1;
DictIndex++;
}
else
FoundInDict = false;
}

for(int k = 0; k < DictIndex; k++)
{
if(Dictionary[k].Appearance == 1)
{
cout << "Your partner has been found:" << Dictionary[k].Item << endl;
break;
}
}

}
星羽 2007-09-26
  • 打赏
  • 举报
回复

#include "iostream"
#include "map"
#include "string"

using namespace std;

int main()
{
string str;
cout<<"enter you string : ";
cin>>str;

map<char, int> mapStr;
for (size_t i = 0; i < str.size(); ++i)
mapStr[str[i]]++;

map<char, int>::iterator itor = mapStr.begin();
for (; itor != mapStr.end(); ++itor)
{
if (itor->second == 1)
break;
}
if (itor != mapStr.end())
cout<<"the first char is : "<<itor->first<<endl;
else
cout<<"no char"<<endl;

return 0;
}
冷月清晖 2007-09-26
  • 打赏
  • 举报
回复
有错误,明天再调,下班了
冷月清晖 2007-09-26
  • 打赏
  • 举报
回复
参考:
#include <stdio.h>
#define _SIZE 10

char check(char * in_ch,int size)
{
char *temp=new char[size];
int i=0,result=0;
while (*in_ch)
{
(*(temp+i))=(*in_ch);
in_ch++;
if ((*(temp+result))==(*in_ch))
{
result++;
}
i++;
}
return (*(temp+result));
}
void main()
{
char ch[_SIZE]="abaccdeff";
printf("%c",check(ch,_SIZE));
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧