LoginSignup
1
0

More than 1 year has passed since last update.

AtCoder備忘録

Last updated at Posted at 2021-12-24

入力

input.cpp
// 入力を区切りたいときはscanf
scanf("$d.%d", &a, &b);   // 「5.5」を「5 5」に区切れる 

// 各行の長さが違う二次元配列の入力
vector<vector<int>> a(n);
rep(i,n){
    // 入力数がLで与えられ、そのあとに各要素の入力がある
    int L;cin>>L; a[i].resize(L);
    rep(j,l) cin>>a[i][j];
}

※ cinはデータ数に関わらずO(1)

出力

printf.cpp
// 小数の桁数指定
printf("[%.3f]", 123.45678);   // [123.456]  (桁指定)
printf("[%08.3f]", 123.45678);  // [0123.456] (リーディングゼロ)

STL

vector

vector.cpp
vector<int> v(N, INF);  //INFで初期化
v.resize(n);  // サイズをnに初期化
v = {};  // 配列を空にする
v = { 0 };  // 0クリア

v.push_back(10)  // 末尾に追加
v.pop_back(); // 末尾を削除
v.back() // 末尾を参照
v.back() = 44 // 末尾を上書き

swap(s[a],s[b]);  // 入れ替え
count(v.begin(), v.end(), x) // xの個数
find(a.begin(), a.end(), x)  // xのイテレータ
*min_element(A.begin(), A.end()); // 配列の最小値を求める 

多次元配列

array.cpp
// vectorを使わない場合
int array[n][m];  rep(i,n) rep(j,m) array[i][j] = 0;

// int型の2次元配列(3×4要素の)の宣言 (中身はint、初期値0)
vector<vector<int>> data(3, vector<int>(4, 0));

string & char

string_char.cpp
string s(3, 'a');  // aaa
string s = ""; s += a[0];  // strにcharを代入

s.size();
s1 == s2  // 比較
str + str + c // 結合

###int → string
string s = to_string(num);

###str → int
int n = stoi(s); // stollはll型。
int n = s[0]-'0';  // '0'の文字コードを引く

s.substr(a,b) // s[a]以降をb文字取得 (bは省略可)
s.substr(s.size()-n)  // 最後のn文字取得

s.erase(a, b)  // s[a]以降をb文字削除。bは省略可
s.erase(s.size()-n)  // 最後のn文字削除

s.pop_back(); //最後を削除
s.insert(3, "IN")  //[3]にINを挿入、O(N)
s.find("a")  //最初のaが何番目か。なければstring::nposが返る
s.replace(2, 3, "W")  // [2]以降3文字をWに置換。  asdfjkl → asWkl

//文字かどうか判定できる
if(s[0] >= 'a' && s[0] <= 'z')  //islower(s)
if(s[0] >= 'A' && s[0] <= 'Z')  //isupper(s)

set:重複するデータを保持しない + 昇順ソート

set.cpp
set<int> st;  // int以外にもstring,vector,pairなども可能

st.insert(a);  // 要素の追加
st.size() // 要素数
st.find(x) // xのイテレータを返す
for (auto x : st) // 要素の取得。

map 重複しないキーと対応する値を保持 + 昇順ソート

map.cpp
// 複数ある文字列から、一番登場数が多い文字列を出力
map<string, int> mp;
rep(i,n){ cin >> s; mp[s]++;}  // 入力sの数を数える。勝手に初期化されてる。
int mx = -1;
for(auto p : mp) maxs(mx, p.second);
for(auto p : mp) if(p.second == mx) ans = p.first;
cout << ans << endl;

pair/tupleとauto

pair.cpp
pair<string, int> p("abc", 3);
cout << p.first << "__" << p.second << endl;  // abc__3

p = make_pair("*", 1);
auto p = make_pair(1,2);  // autoで型推定
sort(p.begin(), p.end());  // firstの昇順、同値ならsecondの昇順

// tupleはpairを一般化したもの
tuple<int, string, bool> data(1, "hello", true);
cout << get<2>(data)

// 配列aにpairを追加する。
a.pb( {x,y} ); a.pb(P(x,y));

priority_queue

priority_queue.cpp
// 最大or最小 の要素を取得
priority_queue<int> pq;
pq.push(a);  // 追加 O(logN)
pq.top();  // 最大or最小を取得
pq.pop();  // 最大or最小を削除

・数学編

math.cpp
abs(x);  // (x の絶対値)
pi = acos(-1); //-1のときのcos(x)
sin(x / 180.0 * pi)  // double 型。(π/6) 
__gcd(a, b)  // (a と b の最小公倍数)
a / gcd(a, b) * b   // (a と b の最大公約数)

min(a, b);
min({a1, a2, ..., an}); // 複数のときは{}
swap(a, b);

reverse(str.begin(), str.end()) 
sort(a + 1, a + 5, greater<int>());  // 範囲指定

stack<int> a;  queue<int> b;
a.push(1);
int x1 = a.top();  // 見るだけ
a.pop();  //queueの場合は.front
int x4 = a.size();
if (a.empty())
map<int, int> M1;

priority_queue  // 大きい値のみ取り出したりできる
priority_queue<int, vector<int>, greater<int>> que;  // intの小さい値を取り出す
priority_queue<double, vector<double>, less<int>> que;  // doubleの大きい値

lower_bound(a+l, a+r, x) - a
//a[l] から a[r-1] までの中で初めて x 以上(一番左側)となるインデックスを返します
//upper_boundは、xより大きいイテレータを返す(個数を求められる)
//あとでa[lb]で値が出る

binary_search(a.begin(), a.end(), x)  // xがあればTrue

__builtin_popcount(x)
//整数 x を二進数で表したとき、1 であるようなビットの個数を返す

//bitset
//ビット集合を表す型。N 桁の 2 進数。ビット演算を高速に。
bitset<8> bs2(131);  // 8桁の131。
bitset<8> bs3("10000011"); 

数値

number.cpp
// intは40億。4*10^9

double d = 1.0/n  // 1.0なら答えも小数になる。

// ルートが整数かどうか
int s = sqrt(sq)+0.5  // 切り捨てなので四捨五入する
if(s*s == sq)

n<<1 // (2倍)
1<<n // (2^n)

// べき乗
pow(K - 1, N - 1);
1 << n;  // 2のn乗

文法

for.cpp
for(int x : a)
for(char c : str)

//2重ループをgotoで抜ける
{
  if () {
    goto f;
  }
}
f:

その他

・XOR
cout << (a^b) << endl; //()をつける

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0