sublime自定义代码段

( 不定期更新 ) 可能有比较常用的东西就写成代码段保存咯, 十分好用的小技巧

一方面记录一下自己地代码段, 一方面说说方法

方法 : 工具---插件开发---新建代码片段---设置---保存(后缀改为.sublime-snippet)

名称 功能
cf 生成一套cf模板
for2 两层for循环
tobit 十进制转二进制
toint 二进制转十进制
kmp kmp

cf

<snippet>
	<content><![CDATA[
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	ios::sync_with_stdio(false);
    cin.tie(0);

	return 0;
}
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>cf</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.c++</scope>
</snippet>

for2

<snippet>
	<content><![CDATA[
for(int i = 0; i < ${1:n}; ++i) {
	for(int j = 0; j < ${2:m}; ++j) {
		${3:/*code*/}
	}
}
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>for2</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.c++</scope>
</snippet>

tobit

<snippet>
	<content><![CDATA[
string tobit(long long x, int m) { 	// 作用 : 十进制数(x)转二进制字符串(m位), 前补0
	string s(m,'');
	for(int i = m-1; i >= 0; --i) 
	{ 
	    s[i] = (x & 1) + '0';
	    x >>= 1;
	}
	return s;
}
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>tobit</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.c++</scope>
</snippet>

toint

<snippet>
	<content><![CDATA[
long long toint(string s) { // 作用 : 二进制串转十进制数 (long long)
	long long x = 0;
	int m = s.size();
	for (int j = 0; j < m; ++j)
	{
	    x = x*2 + s[j] - '0'; // 二进制s --> 十进制x
	}
	return x;
}
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>toint</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.c++</scope>
</snippet>

kmp

<snippet>
	<content><![CDATA[
/* kmp 算法
next : 用于求next数组, 返回next数组, 注意"真正的next数组是这里next的index = 1-n, 用于kmp的是0-(n-1)"
kmp : 用于求kmp, 返回s2在s1中出现的位置(头部)
*/
vector<int> getnext(string s) {
	vector<int> next;
	next.push_back(-1);
	int i = 0, j = -1;	// j指向公共前后缀前缀的最后一个元素上, 表示最长公共前后缀长度
	while(i < s.size()) {
		if(s[i] == s[j] || j==-1) {
			i++;
			j++;
			next.push_back(j);
		}
		else {
			j = next[j]; // 如果s[i]!=s[j]说明匹配失败, 回到上一级公共前后缀处
		}
	}
	return next;
}

vector<int> kmp(string s1, string s2) {	// kmp : 找出s2在s1中出现的位置(全部)
	vector<int> next = getnext(s2);
	vector<int> ans;
	int i = 0, j = 0; 	// i指s1, j指s2
	while(i < s1.size()) {
		if(s1[i] == s2[j] || j==-1) {
			if(j == s2.size()-1) { // 如果匹配完了s2
				ans.push_back(i - j); // ans += s2首部在s1中的位置
				j = next[j];
			}
			else {
				i++;
				j++;
			}
		}
		else {
			j = next[j];
		}
	}
	return ans;
}

]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>kmp</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.c++</scope>
</snippet>

原文地址:https://www.cnblogs.com/roccoshi/p/13093461.html