c_cpp 提高cin和cout IO速度

`cin`和`cout`之所以效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段语句可以来打消`iostream`的输入输出缓存,节省了许多时间,使效率与scanf与printf相差无几。<br/> <br/> [关于ios :: sync_with_stdio(false);和cin.tie(0)加速c ++输入输出流](https://www.cnblogs的.com / PrayG / p / 5749832.html)

io.cpp
#include <iostream>
using namespace std;

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

c_cpp SUBSTR()

substr.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	string str = "We think in generalities, but we live in details.";
	// (quoting Alfred N. Whitehead)

	string str2 = str.substr(3, 5);
	cout << str2 << endl;				// "think"

	size_t pos = str.find("live");		// position of "live" in str
	string str3 = str.substr(pos);		// get from "live" to the end
	cout << str3 << endl;				// live in details.

	return 0;
}

c_cpp DAG最长路径

```美人鱼<br/>图LR <br/>开始 - >停止<br/>```

DAG.cpp

c_cpp 最长的对称子串

最长回文子串<br/> <br/> ## Q <br/> <br/>求字符串`S`的最长回文子串的长度。<br/> <br/>``` <br/>样本输入:<br/> PATZJUJZTACCBCC <br/>样本输出:<br/> 9(ATZJUJZTA)<br/>``` <br/> <br/> ## A <br/> <br/> - `dp [i] [j]`:`S [i]`至`S [j]`所表示的子串是否是回文子串,是则是`1`,不是则为`0 `。<br/> - `return max(dp)`<br/> - `dp [i] [j] = dp [i + 1] [j - 1],S [i] == S [j] `<br/> - `dp [i] [j] = 0,S [i]!= S [j]`<br/> - `dp [i] [i] = 1,dp [i] [i +1] =(S [i] == S [i + 1])? 1:0` <br/> - `O(n ^ 2)`<br/> <br/> --- <br/> <br/>最长回文子串问题还有一些其他做法,例如`二分+字符串hash`复杂度为`O(n logn)`,最优秀的当属复杂度为`O(n)`的`Manacher`算法。<br/> <br/>

LSS.cpp
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int N;
const int MAX_N = 1e3 + 5;
string S;
int dp[MAX_N][MAX_N];

int main() {
	getline(cin, S);
	int len = S.size(), ans = 1;
	memset(dp, 0, sizeof(dp));

	// boundary
	for (int i = 0; i < len; i++) {
		dp[i][i] = 1;
		if (i < len - 1) {
			if (S[i] == S[i + 1]) {
				dp[i][i + 1] = 1;
				ans = 2;
			}
		}
	}

	// state transition equation
	for (int l = 3; l <= len; l++) {
		for (int i = 0; i + l - 1 < len; i++) {
			int j = i + l - 1;
			if (S[i] == S[j] && dp[i + 1][j - 1] == 1) {
				dp[i][j] = 1;
				ans = l;
			}
		}
	}

	cout << ans << endl;
	return 0;
}

/*
Sample Input:
PATZJUJZTACCBCC
Sample Output:
9 (ATZJUJZTA)
*/

c_cpp 最大子序列和

最大连续子序列和<br/> <br/> ## Q <br/> <br/>给定一个数字序列`A1,A2,...,An`,求`i,j(1 <= i <= j <= n)`,使得`Ai + ... + Aj`最大,输出这个最大和。<br/> <br/>``` <br/>样本输入:<br/> 6 <br/> -2 11 -4 13 -5 -2 <br/>样本输出:<br/> 20(11 +( - 4)+ 13 = 20)<br/>``` <br/> <br/> ## A <br/> <br/> - `dp [i]`:**以'A [i]`结尾**的连续序列的最大和<br/> - `return * max_element( dp,dp + n)`<br/> - `dp [i] = max {A [i],dp [i - 1] + A [i]}`<br/> - `dp [0] = A [0]`<br/> - `O(n)`,借助`STL`可以降到`O(n logn)`<br/> <br/>``` <br/> 0 1 2 3 4 5 <br/> -2 11 -4 13 -5 -2 <br/>``` <br/> <br/>``` <br/> dp [0] = -2 <br/> dp [ 1] = 11 <br/> dp [2] = 11 +( - 4)= 7 <br/> dp [3] = 7 + 13 = 20 <br/> do [4] = 20 +( - 5) = 15 <br/> dp [5] = 15 +( - 2)= 13 <br/>``` <br/>

MSS.cpp
#include <iostream>
#include <algorithm>
using namespace std;

int N;
const int MAX_N = 1e5 + 5;
int A[MAX_N], dp[MAX_N];

int main() {
	cin >> N;
	for (int n = 0; n < N; n++) {
		cin >> A[n];
	}

	// boundary 
	dp[0] = A[0];
	
	// state trasition equation 
	for (int i = 1; i < N; i++) {
		dp[i] = max(A[i], dp[i - 1] + A[i]);
	}

	cout << *max_element(dp, dp + N) << endl;
	return 0;
}

/*
Sample Input:
6
-2 11 -4 13 -5 -2
Sample Output:
20 (11 + (-4) + 13 = 20)
*/

c_cpp dcmotor_helloworld.ino

dcmotor_helloworld.ino
/**
 * DCモーターを3秒ごとに再生と停止を繰り返すサンプル
 */
void setup() 
{
  pinMode(11, OUTPUT);
}

void loop() 
{
  digitalWrite(11,HIGH);
  delay(3000); 
  digitalWrite(11,LOW);
  delay(3000);
}

c_cpp 格式化输出小数

##引用<br/> <br/> - [C ++ - cout输出小数点后指定位数](https://blog.csdn.net/edricbjtu/article/details/41082597)<br/>

cout_double.cpp
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	double d = 0.200001;
	cout << setprecision(8) << d << endl;			// 0.200001
	cout.setf(ios::fixed);							// 开启补 0 
	cout << fixed << setprecision(8) << d << endl;	// 0.20000100
	cout.unsetf(ios::fixed);						// 关闭补 0 
	cout << setprecision(8) << d << endl;			// 0.200001
	return 0;
}

c_cpp 数塔问题

## Q <br/> <br/>把一些数字排成数塔的形状,第`i`层有`i`个数字(`1 <= i <= n`)。从第一层走到第n'层,每次只能走向下一层连接的两个数字中的一个,将路径上的所有数字相加后得到的和最大是多少❓<br/> <br/>`` <br/> 15 <br/> / \ <br/> / \ <br/> 8 3 <br/> / \ / \ <br/> / \ / \ <br/> 4 17 6 <br/> / \ / \ / \ <br/> / \ / \ / \ <br/> 4 19 13 4 <br/> / \ / \ / \ / \ <br/> / \ / \ / \ / \ $ <br/> 9 25 43 49 1 <br/>``` <br/> <br/> ## A <br/> <br/> - `dp [i] [j]`:从第`i`层,第`j`个数字走到最底层得到的最大和<br/> - `return dp [1] [1]` <br/> - `dp [i] [j] = max(dp [i + 1] [j],dp [i + 1] [j + 1])+ f [i] [j];`<br/> - `dp [n] [j] = f [n] [j],1 <= j <= n` <br/> <br/>

number_tower.cpp
#include <iostream>
#include <algorithm>
using namespace std;

const int MAX_N = 1e3;
int f[MAX_N][MAX_N], dp[MAX_N][MAX_N];
int N;

int main() {
	cin >> N;
	for (int n = 1; n <= N; n++) {
		for (int j = 1; j <= n; j++) {
			cin >> f[n][j];
		}
	}
	/*
	for (int n = 1; n <= N; n++) {
		for (int j = 1; j <= n; j++) {
			cout << f[n][j] << " ";
		}
		cout << endl;
	}
	// */

	// boundary
	for (int j = 1; j <= N; j++) {
		dp[N][j] = f[N][j];
	}
	// state transition euqation
	for (int i = N - 1; i >= 1; i--) {
		for (int j = 1; j <= i; j++) {
			dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + f[i][j];
		}
	}

	cout << dp[1][1] << endl;
	return 0;
}

/*
Sample Input:
5
5
8 3
12 7 16
4 10 11 6
9 5 3 9 4
Sample Output:
44
*/

c_cpp 数塔问题

## Q <br/> <br/>把一些数字排成数塔的形状,第`i`层有`i`个数字(`1 <= i <= n`)。从第一层走到第n'层,每次只能走向下一层连接的两个数字中的一个,将路径上的所有数字相加后得到的和最大是多少❓<br/> <br/>`` <br/> 15 <br/> / \ <br/> / \ <br/> 8 3 <br/> / \ / \ <br/> / \ / \ <br/> 4 17 6 <br/> / \ / \ / \ <br/> / \ / \ / \ <br/> 4 19 13 4 <br/> / \ / \ / \ / \ <br/> / \ / \ / \ / \ $ <br/> 9 25 43 49 1 <br/>``` <br/> <br/> ## A <br/> <br/> - `dp [i] [j]`:从第`i`层,第`j`个数字走到最底层得到的最大和<br/> - `return dp [1] [1]` <br/> - `dp [i] [j] = max(dp [i + 1] [j],dp [i + 1] [j + 1])+ f [i] [j];`<br/> - `dp [n] [j] = f [n] [j],1 <= j <= n` <br/> - `O(n ^ 2)`<br/>

number_tower.cpp
#include <iostream>
#include <algorithm>
using namespace std;

const int MAX_N = 1e3;
int f[MAX_N][MAX_N], dp[MAX_N][MAX_N];
int N;

int main() {
	cin >> N;
	for (int n = 1; n <= N; n++) {
		for (int j = 1; j <= n; j++) {
			cin >> f[n][j];
		}
	}
	/*
	for (int n = 1; n <= N; n++) {
		for (int j = 1; j <= n; j++) {
			cout << f[n][j] << " ";
		}
		cout << endl;
	}
	// */

	// boundary
	for (int j = 1; j <= N; j++) {
		dp[N][j] = f[N][j];
	}
	// state transition euqation
	for (int i = N - 1; i >= 1; i--) {
		for (int j = 1; j <= i; j++) {
			dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + f[i][j];
		}
	}

	cout << dp[1][1] << endl;
	return 0;
}

/*
Sample Input:
5
5
8 3
12 7 16
4 10 11 6
9 5 3 9 4
Sample Output:
44 (5 → 3 → 16 → 11 → 9)

Sample Input:
5
15
8 3
4 17 6
4 19 13 4
9 25 43 49 1
Sample Output:
102 (15 → 8 → 17 → 19 → 43)
*/

c_cpp 布设()

- `map`容器的成员函数`emplace()`可以在适当的位置直接构造新元素,从而**避免复制和移动操作**。<br/> <br/> - 只有当容器中现有元素的键与这个元素的键不同时,才会构造这个元素。<br/> <br/> --- <br/> <br/> - 成员函数`emplace()`和`insert()`返回的'pair`对象提供的指示相同。<br/> <br/> - `对`的成员变量`first`是一个指向插入元素或阻止插入的元素的迭代器; <br/> <br/> - 成员变量`second`是个布尔值,如果元素插入成功,`second`就为`true`。<br/> <br/>``` <br/> map <Name,size_t> people; <br/> auto pr = people.emplace(Name {“Dan”,“Druff”},77); <br/> auto iter = people.emplace_hint(pr.first,Name {“Cal”,“Cutta”},62); <br/>``` <br/> <br/>

emplace.cpp
#include <iostream>
#include <utility>
#include <string>
#include <map>
using namespace std;

int main() {
	map<string, string> m;

	// uses pair's move constructor
	m.emplace(make_pair(string("a"), string("a")));	// a => a

	// uses pair's converting move constructor
	m.emplace(make_pair("b", "abcd"));				// b => abcd

	// uses pair's template constructor
	m.emplace("d", "ddd");							// d => ddd

	// uses pair's piecewise constructor
	m.emplace(piecewise_construct,
		forward_as_tuple("c"),
		forward_as_tuple(10, 'c'));					// c => cccccccccc
	// as of C++17, m.try_emplace("c", 10, 'c'); can be used

	for (const auto& p : m) {
		cout << p.first << " => " << p.second << '\n';
	}
}