整理的算法模板合集: ACM模板
目录
Section 1.1 介绍
USACO Training 综述
http://train.usaco.org/usacogate(USACO 97道题)
USA Computing Olympiad 是美国高校的信息学测评网站,也是美国中学生的官方竞赛网站。
美国著名在线题库,专门为信息学竞赛选手准备。
全英文界面,但有非官方的中文翻译。推荐直接阅读英语原文,既准确可靠又可提高英语水平。
网站的Training题目全面,每道题附有详细题解,可查看测试数据和运行结果,便于调试、发现错误并改正。采用章节递进的层次结构,由易到难,讲授知识、练习编程结合,题目必须依次完成,避免了只挑简单题做的行为。
第一章较为简单,属于竞赛入门题。基本不涉及太多算法和技巧,请在一周内刷完本章习题,用来巩固基础,并开始下一章的练习。
Section 1.2 提交解决方案,任务类型,特殊问题
luoguP1200、P1201、P1202、P1203
1.2.1 AcWing 1339. 你的旅途由此开始(字符串模拟)
经 典
简单的字符串模拟
#include <cstdio>
#include <algorithm>
#include <iostream>
std :: string s1, s2;
const int mod = 47;
int main()
{
std :: cin >> s1 >> s2;
int mul1 = 1, mul2 = 1;
for(int i = 0; i < s1.length(); ++ i)
mul1 *= (s1[i] - 'A' + 1) % mod;
for(int i = 0; i < s2.length(); ++ i)
mul2 *= (s2[i] - 'A' + 1) % mod;
if(mul1 % mod == mul2 % mod)puts("GO");
else puts("STAY");
return 0;
}
1.2.2 AcWing 1340. 贪婪的送礼者(模拟,map)
模拟,直接用map存名字和当前的钱数,每次送钱的时候,自己能剩下的就等于 money - (money / num) * num
,注意如果num = 0
需要特判一下。剩下的直接模拟就行了
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
const int N = 50007;
int n, m;
map<string, int>mp;
string names[N];
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; ++ i){
string s;
cin >> s;
names[i] = s;
mp[s] = 0;
}
for(int i = 1; i <= n; ++ i){
int money, num;
string s;
cin >> s;
scanf("%d%d", &money, &num);
int remain = 0;
if(num != 0)remain = money - (money / num) * num;
money -= remain;
mp[s] -= money;
for(int j = 1; j <= num; ++ j){
string name;
cin >> name;
mp[name] += money / num;
}
}
for(int i = 1; i <= n; ++ i){
cout << names[i] << " " << mp[names[i]] << endl;
}
return 0;
}
1.2.3 AcWing 1341. 十三号星期五(日期模拟)
经典日期模拟题目。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int month[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int weekday[7];
int main()
{
int n;
cin >> n;
int year = 1900, days = 0;
while (n -- )
{
for (int i = 1; i <= 12; i ++ )
{
weekday[(days + 12) % 7] ++ ;
days += month[i];
if (i == 2)
if (year % 4 == 0 && year % 100 || year % 400 == 0)
days ++ ;
}
year ++ ;
}
for (int i = 5, j = 0; j < 7; i ++, j ++ )
cout << weekday[i % 7] << ' ';
return 0;
}
基姆拉尔森计算公式:
w e e k = ( d a y + 2 × m o n t h + [ 3 × ( m o n t h + 1 ) ÷ 5 ] + y e a r + [ y e a r ÷ 4 ] − [ y e a r ÷ 100 ] + [ y e a r ÷ 400 ] + 1 ) m o d 7 week=(day+2×month+[3×(month+1)÷5]+year+[year÷4]−[year÷100]+[year÷400]+1)mod7 week=(day+