First non-repeating character in a stream
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的更多相关文章
- Leetcode: Longest Repeating Character Replacement && G 面经
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- G 面经 && Leetcode: Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- LeetCode——Longest Repeating Character Replacement
1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...
- length of the longest substring without repeating character
Given a string, find the length of the longest substring without repeating characters. 来源:力扣(LeetCod ...
- leetcode424 Longest Repeating Character Replacement
""" Given a string s that consists of only uppercase English letters, you can perform ...
- [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- LeetCode 424. Longest Repeating Character Replacement
原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...
- 3. Longest Substring Without Repeating Character[M] 最大不重复子串
题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...
随机推荐
- arm学习——有关位操作的总结
在学习arm的过程中,感觉寄存器,基本不会提供位操作,而是整体的操作, 整体操作的就是要注意在对某位赋值的时候不要影响到其他位,看上去不简单, 其实,整体操作有技巧, 那么就来总结一下: 1.首先要理 ...
- 【第八课】php-fpm.conf配置文件解析
在discuz论坛的nginx配置文件当中,我们可以看到有一段php解析的配置,如下: location ~ \.php$ { try_files $uri = 404; fastcgi_pass 1 ...
- 微信小程序之自定义组件的应用
小程序支持自定义组件,下面是一个简单的购物车组件,实现的效果如图: 效果图 创建组件 在根目录创建components目录,然后创建计数组件 count 如图: 组件内容 <!--compone ...
- Linux下tomcat的启动,关闭,以及shutdown失败杀死进程的方法
1.tomcat服务器第一次启动并查看启动日志的命令 在 ../bin 文件夹下输入./startup.sh;tail -f ../logs/catalina.out 2.需要重启服务器的时候 在 . ...
- java保留两位小数4种方法(转载)
喵喵最近经常遇到小数点保留的问题,转载一篇Java里面的几种小数点位数控制方法. 这是转载的原地址:https://www.cnblogs.com/chenrenshui/p/6128444.html ...
- Cooperate with Myself
(一) 第一周的第一批作业们. 且不说一周之内要看完我们的300多页的教材,也不说需要在维基的大批量的文献中海底捞针,单是这个四则运算的生成程序就让我从假期的迷糊状态中幡然觉悟了:哦!惊险刺激的新的 ...
- 0302-对IT行业的感思
在参考并分析了2014行业排名和IT行业的就业分析后,给我的第一体会就是:如今的IT行业,是一个机会与挑战并存的行业. 先说机会. 从行业排行相关资料不难看出,现在是一个信息与大数据引领一切的时代,电 ...
- beta 圆桌桌 4
031602111 傅海涛 1.今天进展 后台接口大部分完善,并完成交互 2.存在问题 部分接口不稳定 3.明天安排 完成全部接口的交互 4.心得体会 小问题真多,要一个一个解决 031602115 ...
- 各小组Alpha版项目发布作品点评
第一组:新蜂小组 题目:俄罗斯方块 评论:主体功能已经完成,可以流畅的进行游戏,游戏素材都是由贴图美化过的,期待计分系统等的完善. 第二组:天天向上 题目:连连看 评论:核心功能完成,可以流畅的进行游 ...
- 在 SQL Server 中从完整路径提取文件名(sql 玩转文件路径)
四个函数: --1.根据路径获取文件名 -- ============================================= -- Author: Paul Griffin -- Crea ...