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. HW 2017 12 17可禾大佬神题

    好不容易搞来的题目,不写一写怎么行呢. 不过难度真心不高(一小时K掉),都是老题+暴力题,没有欧洲玄学. 再说一句,这试卷是叶可禾出的吧. T1 好老的题目,看到有多组数据我还怕了,以为有更流弊的算法 ...

  2. Java入门知识1

    1. 获取标准屏幕的输入时,需导入java.util.Scanner包. 2. 主类的名称与.java的文件名需一致. 3. 文件中主类设置为public,其他类前面无需加访问级别. 4. 继承时,使 ...

  3. Egret(白鹭引擎)——Egret+fairyGui 实战项目入门

    前言 一行白鹭上青天 需求 最近,我们老板刷刷的为了省事,给美术减压(背景有点长,不说了). 美术出 fairygui,我需要在网页上看到实时操作,并且看到效果! 需求分析 这怕是要了我的狗命啊,但是 ...

  4. REST-framework快速构建API--初体验

    一.快速上手 1.环境准备 安装restframework,注册app pip install djangorestframework INSTALLED_APPS = [ 'django.contr ...

  5. golang高性能端口扫描

    前言 最近有个小项目的需要,使用golang写了个端口扫描工具,不得不说golang的效率确实比python快的太多了.在使用一段时间golang之后,感觉有三个方面是优于python的: 一个方面是 ...

  6. flask入门小方法

    我是在pycharm中写的.那么需要在Termainal中cd 到当前文件所在的文件夹,在运行python py文件名 一开始想用面向对象的方法来封装这些小模块,但发现在面向对象中要用到类属性,以及类 ...

  7. kubeadm安装K8S单master双节点集群

    宿主机:master:172.16.40.97node1:172.16.40.98node2:172.16.40.99 # 一.k8s初始化环境:(三台宿主机) 关闭防火墙和selinux syste ...

  8. 以e2e_cli为例漫谈fabric的一些基础知识点

    在刚接触fabric的时候一般都是直接跟着wiki的教程一步步安装配置,执行一系列命令,最终将其运行起来,但很多人对其中的运行流程及其基础知识点可能不是很了解.基于此今天我将以$FABRIC_ROOT ...

  9. 第二次Scrum meeting

    第二次Scrum meeting 任务及其要求: 成员 12.11 12.12 陈谋 完成Tags的爬取工作(已完成) stackoverflow的问题抽取 卢惠明 视频链接的挖掘和整理(未完成) 视 ...

  10. 第二阶段冲刺——two

    个人任务: 王金萱:优化作业查询结果,按学号排列. 马佳慧:测试登录功能并优化. 司宇航:修复博客作业查询功能. 季方:测试博客作业查询功能. 站立会议: 任务看板和燃尽图: