Skip to content

Instantly share code, notes, and snippets.

@ititorit
Created September 22, 2016 15:05
Show Gist options
  • Select an option

  • Save ititorit/2c2d265e1817db934e2baf8de39b1713 to your computer and use it in GitHub Desktop.

Select an option

Save ititorit/2c2d265e1817db934e2baf8de39b1713 to your computer and use it in GitHub Desktop.
SPOJ CPTTRN2
// contest: http://www.spoj.com/problems/CPTTRN2/
// author: Nguyen Minh Tuan.
// Language: C/C++.
#include <bits/stdc++.h>
#define _for(i,a,b) for(int i=(a),_b_=(b),_d_=(a<b?1:-1);i!=_b_;i+=_d_)
#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define _it(i,v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define _all(v) v.begin(), v.end()
#define DEBUG(x) { cout << #x << " = " << x << endl; }
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// __gcd algorithm
ll gcd(ll a, ll b) {
return b ? gcd(b, a%b) : a;
}
// end __gcd
// power modulo algorithm
ll power_modulo(ll a, ll base, ll mod) {
if(!base) { return 1; }
ll temp = power_modulo(a, base / 2, mod);
if(base & 1) return a * sqr(temp) % mod;
return sqr(temp) % mod;
}
// end power modulo
// quick sort algorithm
void quick_sort(int A[], int l, int r) {
int i = l, j = r;
int pivot = A[(l + r) / 2];
if(l >= r) return;
while(i <= j) {
while(A[i] < pivot) i++;
while(A[j] > pivot) j--;
if(i <= j) swap(A[i++], A[j--]);
}
quick_sort(A, l, j); quick_sort(A, i, r);
}
// End quick sort algorithm
// int_To_String algorithm
string int_To_String(int i)
{
stringstream ss;
ss << i;
return ss.str();
}
int string_To_int(string s) {
return atoi(s.c_str());
}
// end int_To_String algorithm
// KMP algorithm
int KMP(string s, string t) {
int n = s.length(), m = t.length();
int* pit = new int[m+1];
if(m >= 0) pit[0] = 0;
if(m >= 1) pit[1] = 0;
FOR(i, 2, m) {
for(int j = pit[i-1]; ;j = pit[j]) {
if(t[j] == t[i-1]) { pit[i] = j + 1; break; }
if(j == 0) { pit[i] = 0; break; }
}
}
for(int i = 0, j = 0; i < n; ) {
if(s[i] == t[j]) {
i++; j++;
if(j == m) return i - m;
} else if(j > 0) {
j = pit[j];
} else i++;
} delete[] pit; return -1;
}
// end KMP algorithm
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// solution starts...
int test, row, col, cnt;
cin >> test;
while(test--) {
cin >> row >> col;
FOR(i,1,row) {
FOR(j,1,col) {
if((i > 1 && i < row) && (j > 1 && j < col)) cout << ".";
else cout << "*";
}
cout << "\n";
}
cout << endl;
}
// solution ends...
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment