牛客国庆集训派对Day4 J-寻找复读机

链接:https://www.nowcoder.com/acm/contest/204/J
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld

题目描述

某个 QQ 群里一共有 n 个人,他们的编号是 1..n,其中有一些人本质上是复读机。
小 A 发现,如果一个人的本质是复读机,那么他每次发的消息一定跟群里的上一条消息一样,特别地第一个发消息的人一定不是复读机。
现在小 A 搞到了一份聊天记录,他想请你找出所有可能是复读机的群友

输入描述:

第一行两个正整数 n,m,表示群里的人数和聊天记录的总条数
接下来 m 行按时间顺序给出聊天记录,每行有一个正整数 x 和一个小写字母字符串 S,表示群友 x 发了消息 S

输出描述:

输出一行,将所有可能是复读机的群友的编号按照从小到大排序后输出,每两个编号之间隔一个空格

示例1

输入

3 5
1 gugugu
2 gugugu
1 gugu
3 tingzhifudu
2 tingzhifudu

输出

2

备注:

1≤ n≤ 10^3
1≤ m≤ 10^3
1≤ |S|≤ 100

思路

这题有一个坑点是要找到可能是复读机的群友。所以那些没说过话的也可能是复读机。那么找到已知的一定不是复读机的群友,然后把这些群友排除在外就可以了

AC代码

/*
* @Author: WZY
* @Date:   2018-10-09 16:36:48
* @Last Modified by:   WZY
* @Last Modified time: 2018-10-09 17:52:41
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"
"
const double E=exp(1);
const int maxn=1e6+10;
const int mod=998244353;
using namespace std;
int vis[maxn];
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	    double _begin_time = clock();
	#endif
	int n,m;
	cin>>n>>m;
	string st1,st2;//st2记录上一个群友说的话
	int id;
	while(m--)
	{
		cin>>id>>st1;
		// 如果这次的群友和上一个群友说的话一样
		if(st1==st2)
			continue;
		// 如果不一样
		vis[id]=1;	//表示该id的群友不是复读机
		st2=st1;	//记录这个群友说的话
	}
	int _=0;
	for(int i=1;i<=n;i++)
	{
		if(vis[i])
			continue;
		if(_)
			cout<<" ";
		else
			_++;
		cout<<i;
	}
	cout<<endl;
	#ifndef ONLINE_JUDGE
	    long _end_time = clock();
	    printf("time = %lf ms.", _end_time - _begin_time);
	#endif
	return 0;
}
原文地址:https://www.cnblogs.com/Friends-A/p/10324345.html