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