First non-repeating character in a stream

Given an input stream of n characters consisting only of small case alphabets the task is to find the first non repeating character each time a character is inserted to the stream.

Example

Flow in stream : a, a, b, c
a goes to stream : 1st non repeating element a (a)
a goes to stream : no non repeating element -1 (5, 15)
b goes to stream : 1st non repeating element is b (a, a, b)
c goes to stream : 1st non repeating element is b (a, a, b, c)

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the stream. Then in the next line are x characters which are inserted to the stream.

Output:
For each test case in a new line print the first non repeating elements separated by spaces present in the stream at every instinct when a character is added to the stream, if no such element is present print -1.

Constraints:
1<=T<=200
1<=N<=500

Example:
Input:
2
4
a a b c
3
a a c 
Output:
a -1 b b
a -1 c

如何处理重复是个难点,本题考察的是队列

hashing  linked listqueue

纯字符串处理方法.

 #include <algorithm>
#include <iostream>
#include <string> using namespace std; int main() {//by guxuanqing@gmai.com
int T,N;
cin >> T;
//getchar();
string str;
string result;
string tmpch;
while(T--)
{
cin >> N;//debug(N);
//getchar();
str.clear();
result.clear();
tmpch.clear();
int hashs[] = {};
char ch;
int i = ;
for(i = ; i < N; i++)
{
cin >> ch;//debug(ch);
while (ch == ' ') {
cin >> ch;
}
//getchar();
str.push_back(ch);//debug(str);
++hashs[(int)str[i]]; //debug(hashs[str[i]]);
if(hashs[(int)str[i]] == )
{
if(tmpch.empty())
{
result.push_back(str[i]);
}else
{
result.push_back(tmpch[]);
}
tmpch.push_back(str[i]);//the first time occurs,push it back to tmpch
}else
{
// string::size_type n = tmpch.find(str[i]);debug(tmpch);
// if(n != string::npos)
// {
// tmpch.erase(n,n);debug(tmpch);
// }
auto n = std::find(tmpch.begin(), tmpch.end(), str[i]);
if(n != tmpch.end()) tmpch.erase(n);
if(tmpch.empty())
{
result.push_back('');
}else
{
result.push_back(tmpch[]);
}
}
//debug(result);
}
for(i = ; i < N; i++)
{
if(result[i] == '') cout << "-1 ";
else cout << result[i] << ' ';
}
cout << endl;
} return ;
}

0.093s

 
栈处理的方法:
 //by Amit Negi 2
#include <iostream>
#include<queue>
using namespace std; int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n,i,j;
int arr[];
queue<char> q;
cin>>n;
char s[n];
for(i=;i<n;i++)
cin>>s[i];
for(i=;i<;i++)
arr[i]=;
for(i=;i<n;i++)
{
if(arr[s[i]-'a']==)
{
q.push(s[i]);
arr[s[i]-'a']=;
}
else
arr[s[i]-'a']+=; while(!q.empty()&&arr[q.front()-'a']!=)
q.pop();
if(q.empty())
cout<<-<<" ";
else
cout<<q.front()<<" ";
}
cout<<endl;
}
return ;
}

0.082s

 //by Ayush Bansal 9
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;i++)
using namespace std; int main() {
//code
int t,n;
char c;
cin>>t;
while(t--)
{
vector<int> v(,);
queue<char> q;
cin>>n;
FOR(i,,n)
{
cin>>c;
v[c-'a']++;
if(v[c-'a']<=)
{
q.push(c);
}
char ans;
while(!q.empty())
{
if(v[q.front()-'a']<=)
{
ans=q.front();
break;
}
else
{
q.pop();
}
}
if(q.empty())
{
cout<<-<<' ';
}
else
{
cout<<ans<<' ';
}
}
cout<<endl;
} return ;
}

0.115s

First non-repeating character in a stream的更多相关文章

  1. Leetcode: Longest Repeating Character Replacement && G 面经

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  2. [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  3. G 面经 && Leetcode: Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  4. LeetCode——Longest Repeating Character Replacement

    1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...

  5. length of the longest substring without repeating character

    Given a string, find the length of the longest substring without repeating characters. 来源:力扣(LeetCod ...

  6. leetcode424 Longest Repeating Character Replacement

    """ Given a string s that consists of only uppercase English letters, you can perform ...

  7. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  8. LeetCode 424. Longest Repeating Character Replacement

    原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...

  9. 3. Longest Substring Without Repeating Character[M] 最大不重复子串

    题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...

随机推荐

  1. Command and Query Responsibility分离模式

    CQRS模式,就是命令和查询责任分离模式. CQRS模式通过使用不同的接口来分离读取数据和更新数据的操作.CQRS模式可以最大化性能,扩展性以及安全性,还会为系统的持续演化提供更多的弹性,防止Upda ...

  2. POJ 1068&&2632&&1573&&2993&&2996

    这次的题目是著名的模拟(水逼)专题 题目难度都不大,思维也不深,就是一个字——烦 同时很多题目都有很多坑点 1608 题意是告诉你一个只有()的字符串(且匹配正确),每个)的左边有几个(,让你求每一对 ...

  3. mfc 纯虚函数和抽象类

    纯虚函数 抽像类 一.纯虚函数 虚函数为了重载和多态的需要,有时需要在基类中定义一个纯虚函数,代码部分在子类中加以实现.定义格式如下的函数我们称为纯虚函数: ; 纯虚函数与空虚函数是有区别的; 二.抽 ...

  4. Linux 平台和 Windows平台下 Unicode与UTF-8互转

    Windows: unsigned char * make_utf8_string(const wchar_t *unicode) { , index = , out_index = ; unsign ...

  5. Google是如何教会机器玩Atari游戏的

    转自:http://blog.csdn.net/revolver/article/details/50177219 今年上半年(2015年2月),Google在Nature上发表了一篇论文:Human ...

  6. jvm系列(八):jvm知识点总览

    在江湖中要练就绝世武功必须内外兼备,精妙的招式和深厚的内功,武功的基础是内功.对于武功低(就像江南七怪)的人,招式更重要,因为他们不能靠内功直接去伤人,只能靠招式,利刃上优势来取胜了,但是练到高手之后 ...

  7. jenkins +gitlab +docker 自动化部署tomcat 项目

    实验环境 实验设备 三台服务器 centos 7.X 以上 内存 2-3G左右 192.168.1.195 (jenkins最新+ git 2.8+maven 3.5 +tomcat 8+java1. ...

  8. 1.0.0 Unity零基础入门——打砖块

    1)设置好相应场景 2)创建脚本挂载到相应物体上并编写 2.代码 //Shoot - - 控制小球生成与射击 using System.Collections; using System.Collec ...

  9. 网易云易盾朱星星:最容易被驳回的10大APP过检项

    本文由  网易云发布. 1月20日,“走进网易:移动测试与安全实践”公开活动在杭州西湖区颐高创业大厦4F楼友会创业咖啡厅举行.本次活动的议题聚焦在如何实现应用的高效开发.安全过检.开发功耗降到最低等热 ...

  10. PAT甲题题解-1040. Longest Symmetric String (25)-求最长回文子串

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...