猛攻大模拟day2!

字符串匹配 纯粹的kmp算法,让我顺便又复习了一下,然后加上了一个大小写模糊匹配

代码如下 中间卡了一个点,后面发现是next数组得在m不同状态更新。但acwing上有更简洁得做法,决定学习一下 find函数(服了 何必写这个kmp呢)大小写转换还是一个个换吧 没必要用高级函数(其实就是懒得背了)

#include
using namespace std;
const int N = 105;
string str;
int n, m;
int ne[N];
char tolow(char c)
{
	if ('a' <= c && c <= 'z')return c;
	return c - 'A' + 'a';
}
void work()
{
	ne[0] = -1;
	cin >> str;
	cin >> m >> n;
	for (int i = 1, j = -1; i < str.size(); i++)
	{
		while (j != -1 && str[i] != str[j + 1])j = ne[j];
		if (str[i] == str[j + 1])ne[i] = j + 1, j++;
		else ne[i] = -1;
	}//构造next函数
	if (m == 1)
	{
		for (int i = 1; i <= n; i++)
		{
			string st;
			cin >> st;
			for (int i = 0, j = -1; i < st.size(); i++)
			{
				while (j != -1 && st[i] != str[j + 1])j = ne[j];
				if (st[i] == str[j + 1])j++;
				if (j == str.size() - 1)
				{
					cout << st << endl;
					break;
				}
			}
		}
	}
	else
	{
		for (int i = 1, j = -1; i < str.size(); i++)
		{
			while (j != -1 && tolow(str[i]) != tolow(str[j + 1]))j = ne[j];
			if (tolow(str[i]) == tolow(str[j + 1]))ne[i] = j + 1, j++;
			else ne[i] = -1;
		}//构造next函数
		for (int i = 1; i <= n; i++)
		{
			string st;
			cin >> st;
			for (int i = 0, j = -1; i < st.size(); i++)
			{
				while (j != -1 && tolow(st[i]) != tolow(str[j + 1]))j = ne[j];
				if (tolow(st[i]) == tolow(str[j + 1]))j++;
				if (j == str.size() - 1)
				{
					cout << st << endl;
					break;
				}
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	work();
	return 0;
}

模板生成系统 哎 又没做出来,其实就是对这些数据得存储还不够熟练,没有提前想好怎么去做。

基本知识:getline后字符串可以直接包含空格。用ios得时候,cin和getchar不能混着用

当map中输出不存在键值,会输出空字符串。其他就注意读题吧

#include
#include
#include
#include
using namespace std;
int n, m;
unordered_mapvars;
vector strs;
void work()
{
	cin >> n >> m;
	cin.ignore();
	while (n--)
	{
		string str;
		getline(cin, str);
		strs.emplace_back(str);
	}
	while (m--)
	{
		string key, value;
		cin >> key;
		char c;
		while (c = getchar(), c != '\"');
		while (c = getchar(), c != '\"')value += c;
		vars[key] = value;
	}
	for (auto& str : strs)
	{
		for (int i = 0; i < str.size();)
		{
			if (i + 1 < str.size() && str[i] == '{' && str[i + 1] == '{')
			{
				int j = i + 3;
				string key;
				while (str[j] != ' ')key += str[j++];
				cout << vars[key];
				i = j + 3;
			}
			else cout << str[i++];
		}
		cout << endl;
	}

}
int main()
{
	//ios::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);
	work();
	return 0;
}

路径替换 把当前目录 当成栈 如果返回上一级目录 就弹出 如果没有就压入 这样做就好了

#include
#include
#include
using namespace std;
int n;
vector getpath(string str)
{
	vector ans;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == '/')continue;
		int j = i;
		while (str[j] != '/'&&j cur, vectorpath)
{
	for (auto c : path)
	{
		if (c == ".")continue;
		else if (c == "..")
		{
			if (cur.size())cur.pop_back();
		}
		else cur.push_back(c);
	}
	if (cur.empty())
	{
		cout << '/' << endl;
		return;
	}
	for (auto p : cur)cout << "/" << p;
	cout << endl;
}
void work()
{
	string str;
	cin >> n >> str;
	vector xiangdui = getpath(str);
	vector juedui;
	cin.ignore();
	for (int i = 0; i < n; i++)
	{
		getline(cin, str);
		auto path = getpath(str);
		if (str.size() && str[0] == '/')walk(juedui, path);
		else walk(xiangdui, path);
	}
}
int main()
{
	work();
	return 0;
}

炉石传说 好题 虽然算大模拟里面简单的题目,但是这道题可以学习到很多的知识。

insert可以在vector指定位置(迭代器)之前插入值,或者两个迭代器之间的数组

erase string和vector是不同的。代码如下

#include
#include
using namespace std;
vectorblood1(8,0), blood2(8,0), attack1(8,0), attack2(8,0);
int n;
int state = 1;//默认先手
int cnt1 = 0, cnt2 = 0;
void work()
{
	blood1[0] = blood2[0] = 30;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		string str;
		cin >> str;
		if (str == "end")
		{
			state ^= 1;
		}
		else if (str == "summon")
		{
			int pos, att, hea;
			cin >> pos >> att >> hea;
			if (state)
			{
				blood1.insert(blood1.begin()+pos, hea);
				attack1.insert(attack1.begin() + pos, att);
				cnt1++;
			}
			else
			{
				blood2.insert(blood2.begin() + pos, hea);
				attack2.insert(attack2.begin() + pos, att);
				cnt2++;
			}
		}
		else
		{
			int att, de;
			cin >> att >> de;
			if (state)
			{
				blood1[att] -= attack2[de];
				blood2[de] -= attack1[att];
			}
			else
			{
				blood2[att] -= attack1[de];
				blood1[de] -= attack2[att];
			}
			for (int i = 1; i <= cnt1; i++)
			{
				if (blood1[i] <= 0)
				{
					blood1.erase(blood1.begin()+i);
					attack1.erase(attack1.begin() + i);
					cnt1--;
				}
			}
			for (int i = 1; i <= cnt2; i++)
			{
				if (blood2[i] <= 0)
				{
					blood2.erase(blood2.begin() + i);
					attack2.erase(attack2.begin() + i);
					cnt2--;
				}
			}
		}

	}
	if (blood1[0] <= 0)cout << -1 << endl;
	else if (blood2[0] <= 0)cout << 1 << endl;
	else cout << 0 << endl;
	//cout << "xixi";
	cout << blood1[0] << endl;
	cout << cnt1 << " ";
	for (int i = 1; i <= cnt1; i++)
	{
		cout << blood1[i] << " ";
	}
	cout << endl;
	cout << blood2[0] << endl;
	cout << cnt2 << " ";
	for (int i = 1; i <= cnt2; i++)cout << blood2[i] << " ";
	cout << endl;

}
int main()
{
	work();
	return 0;
}

你可能感兴趣的:(c++)