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: ...
随机推荐
- mfc Unicode转 ASNI ,WCHAR 转 CHAR
知识点: 宽字符转多字节字符 多字节字符转宽字符 什么是ANSI,什么又是UNICODE呢?其实这是两种不同的编码方式标准,ANSI中的字符采用8bit,而UNICODE中的字符采用16bit 在VC ...
- [CF1063F]String Journey[后缀数组+线段树]
题意 在 \(S\) 中找出 \(t\) 个子串满足 \(t_{i+1}\) 是 \(t_{i}\) 的子串,要让 \(t\) 最大. \(|S| \leq 5\times 10^5\). 分析 定义 ...
- Flask学习-Flask app接受第一个HTTP请求
一.__call__() 在Flask app启动后,一旦uwsgi收到来自web server的请求,就会调用后端app,其实此时就是调用app的__call__(environ,start_res ...
- 读取配置文件的URL,使用httpClient发送Post和Get请求,实现查询快递物流和智能机器人对话
1.主要jar包: httpclient-4.3.5.jar httpcore-4.3.2.jar 2.目录结构如图所示: 3.url.properties文件如下: geturl=http:// ...
- Json和Map互转,四个包(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)
目前使用的(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)这四种json-map互转,其他的以后在补充.............. ...
- Kaggle: Google Analytics Customer Revenue Prediction EDA
前言 内容提要 本文为Kaggle竞赛 Google Analytics Customer Revenue Prediction 的探索性分析 题目要求根据历史顾客访问GStore的数据,预测其中部分 ...
- Bloom Filter解析
布隆过滤器简介:https://www.cnblogs.com/Jack47/p/bloom_filter_intro.html 布隆过滤器详解:原文链接:http://www.cnblogs.com ...
- "软件"和"软件工程"一词最早被谁提出?
1."softwar”:1953年Richard R.Carhart在备忘录中使用software一词2.软件工程 1968 年北大西洋公约组织在前联邦德国开会提出的 1968年秋季,NAT ...
- 20135202闫佳歆--week3 课本1-2章学习笔记
第一章 Linux内核简介 一.Unix Unix是一个强大.健壮和稳定的操作系统. 简洁 绝大部分东西都被当做文件对待.这种抽象使对数据和对设备的操作都是通过一套相同的系统调用借口来进行的:open ...
- extjs几个奇怪的错误
在用Extjs进行网页开发的时候,遇见了一下两个错误,这两个错误的位置用firebug调试显示在extjs-all.js Ext.resetElement is undefined g.el is n ...