map的玄学小问题
  • 板块学术版
  • 楼主hex2007
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/7/5 11:19
  • 上次更新2023/10/27 21:51:03
查看原帖
map的玄学小问题
465027
hex2007楼主2022/7/5 11:19

P3879 [TJOI2010] 阅读理解

这个题本用mapmap水掉,结果还调了半天...(已经AC)

原因如下

全WA:

#define NUM 10010

AC代码:

#define NUM 100010

但是这个题的数据范围就是1e41e4啊!!

所以为啥要开到1e51e5才能ACAC嘞?

我的最终代码:

#include<iostream>
#include<cstdio>
#include<map>
#include<stack>
#define NUM 100010
using namespace std;

struct bian{
	int next,zhi;
};
bian a[NUM];
int n,m;
long long cnt;
map <string,int> mp;//存这个单词的头指针,也就是最后一次出现的位置
					//相当于前向星的head 

void add( string s,int p ){ //链式存储 
	if( a[mp[s]].zhi == p ) return; //不可以多次输出同一个短文编号 
	a[++cnt].next = mp[s];
	a[cnt].zhi = p;
	mp[s] = cnt;
}


int main(){
	
	cin >> n;
	int p;string s;
	for( int i = 1;i <= n;i++ ){
		cin >> p;
		for( int j = 1;j <= p;j++ ){
			cin >> s;
			add( s,i );
		}
	}
	
	cin >> m;
	for( int i = 1;i <= m;i++ ){
		cin >> s;
		stack <int> st; //链式是先找到后面的序号再找前面的,所以用栈存答案 
		for( int j = mp[s];j;j = a[j].next )
			st.push(a[j].zhi);//将答案放进栈里 
		while( !st.empty() ){ 
			cout << st.top(); //倒着输出 
			if( st.size() > 1 ) cout << " "; //行末不可以有空格 
			st.pop();
		}
		cout << endl; 
	}
	
	return 0;
}
2022/7/5 11:19
加载中...