每日算法

每日算法

those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.3.25


P1032 字串变换

最后一个数据点卡着时间AC了差了几十毫秒 本来想吸一口氧气的结果不吸氧气也过了 多做绿题名字会一直绿下去嘛QAQ

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <queue>

using namespace std; 

map<string,vector<string> > m;
string a , b;

struct node{
	string s;
	int step;
	node(string s,int a):s(s),step(a){}
};
bool flag = 0;
int bfs()
{
	queue<node> q;
	q.push(node(a,0));
	
	while(!q.empty())
	{
		node now = q.front();
		map<string,vector<string> >::iterator it = m.begin();
		for(it ; it != m.end() ;it ++)
		{
			string t  = it->first;  //变换关系 Ai 
			vector<string> set= it->second;
			for(int i = 0;i <  set.size() ;i ++)
			{
				string tmp = now.s;
				//找到每一个可以替换的位置
			    string s = set[i]; // 要被替换的字符串 Bi
				while(tmp.find(t) != string::npos)
				{
					// 得到前半部分
					int index = tmp.find(t), len = now.s.length();
					string pre  = now.s.substr(0,index);
					string last = now.s.substr(index + t.size(),len - index + t.size());
					string next = pre + s + last;
					//cout << next << endl;
					if(next == b){
						flag = 1;
						return now.step + 1;
					}
					else if(now.step + 1 < 10)q.push(node(next,now.step + 1));
					tmp[index] = '*';
				} 	 
			}
		}
		q.pop();
	}
}
int main()
{
	cin >> a >> b;
	string s1 , s2;
	while(cin >> s1 >> s2)
	{
		m[s1].push_back(s2);
	}
	int ans = 9999;
	ans = bfs();
	if(!flag)
	{
		cout << "NO ANSWER!";
		return 0;
	}else cout << ans << endl;
	return 0;
}

P2251 质量检测

ST表练模板题

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>

using namespace std;
const int N = 1000005; 

int f[N][21] , n , m;

void init()
{
	cin >> n >> m;
	for(int i = 1;i <= n ;i ++)scanf("%d",&f[i][0]);
	for(int j = 1;j <= 21;j ++)
	{
		for(int i = 1;i+(1 <<j) - 1 <= n; i++)
		{
			f[i][j] = min(f[i][j-1],
			f[i + (1 << (j - 1))][j-1]);
		}
	}
}

int query(int l , int r)
{
	int k = log2(r - l + 1);
	return min(f[l][k],f[r-(1 << k) + 1][k]);
}
int main()
{
	init();
	for(int i = 1;i + m <= n + 1;i ++ )
	{
		printf("%d
",query(i,m + i - 1));
	}
	return 0;
}

lqb 全球变暖

STL确实太慢了 不如自己手写的队列快确实是这样啊

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <set>

using namespace std;
const int N = 1005;

char a[N][N];
bool vis[N][N];
int n, dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

bool inmap(int x,int y)
{
	return x >= 1 && x <= n && y >= 1 && y <= n;
}

struct node{
	int x,y;
	node(int x,int y):x(x),y(y){}
};

int block = 0, le = 0 , cnt = 1;

bool check(int x,int y)
{
	for(int i = 0;i < 4;i ++)
	{
		if(a[x + dir[i][0]][y + dir[i][1]] != '#')return false;
	}
	return true;
}

bool flag = false;

void bfs(int x,int y)
{
	queue<node> q;
	q.push(node(x , y));
	vis[x][y] = 1;
	if(check(x,y))flag = 1; 
	while(!q.empty())
	{
		node now = q.front();
	
		for(int i = 0;i < 4;i ++)
		{
			int nx = now.x + dir[i][0];
			int ny = now.y + dir[i][1];
			if(inmap(nx,ny) && !vis[nx][ny] && a[nx][ny] == '#')
			{
				if(!flag){
					if(check(nx,ny))flag = 1;
				}
				vis[nx][ny] = 1;
				q.push(node(nx,ny));
			}
		}
		q.pop();
	}
}

void init()
{
	cin >> n;
	for(int i = 1;i <= n ;i ++)
	{
		for(int j = 1;j <= n ;j ++)
		{
			cin >> a[i][j];
		}
	}
	for(int i = 1;i <= n ;i ++)
	{
		for(int j = 1;j <= n ;j ++)
		{
			if(!vis[i][j] && a[i][j] == '#')
			{
				block++;flag = false;
				bfs(i , j);
				if(flag)le++;
			}
		}
	}
}
int main()
{
	init();
	cout << block - le;
	return 0;
} 
原文地址:https://www.cnblogs.com/wlw-x/p/12576738.html