每日水题 11-24

Frogger

Frogger
最小生成树变形,G++输出要用%f。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e3+5;
int x[N], y[N];
struct node {
  int u, v, w;
  bool operator < (const node & a) const{
    return w < a.w;
  }
}e[N*N];
int dis(int i, int j) {
  return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
}
int f[N];
int Find(int x) {
  return x == f[x] ? x : f[x] = Find(f[x]);
}
int main() {
  int n;
  int ca = 1;
  while(~scanf("%d", &n), n) {
    int cnt = 0, num = n-1, w;
    for(int i = 1; i <= n; ++i) {
      f[i] = i;
      scanf("%d %d", x + i, y + i);
      for(int j = 1; j < i; ++j) {
        e[cnt++] = {i, j, dis(i, j)};
      }
    } 
    sort(e, e + cnt);
    for(int i = 0; i < cnt; ++i) {
      int fx = Find(e[i].u), fy = Find(e[i].v);
      if(fx != fy) {
        f[fx] = fy;
        num--;
        w = e[i].w;
      }
      if(Find(1) == Find(2)) break; 
      if(!num) break;
    }
    printf("Scenario #%d\nFrog Distance = %.3f\n\n", ca++, sqrt(w));
  } 
	return 0;
}

Period

Period
kmp

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second

const int N = 1e6+5;
char s[N];
int nx[N];
void getnx(int n) {
	int k = 0; nx[1] = 0;
	for(int i = 1; i < n; ++i) {
	 while(k && s[i] != s[k]) k = nx[k];
		if(s[i] == s[k]) k++;
		nx[i+1] = k;
	}
	for(int i = 2, len; i <= n; ++i) {
		len = i - nx[i];
		if(nx[i] && i % len == 0) {
			printf("%d %d\n", i, i/len);
		}
	}
	printf("\n");
}

int main() {
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
   int t, ca = 0;
	 while(scanf("%d", &t), t) {
		 scanf("%s", s);	
     printf("Test case #%d\n", ++ca);
		 getnx(t);
	 }
    return 0;
}

Power Strings

Power Strings
循环节长度为n-nx[n] 若可以整除那么就是商,否则是1

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second

const int N = 1e6+5;
char s[N];
int nx[N];
void getnx() {
	int n = strlen(s);
  int k = 0; nx[1] = 0;
	for(int i = 1; i < n; ++i) {
	 while(k && s[i] != s[k]) k = nx[k];
		if(s[i] == s[k]) k++;
		nx[i+1] = k;
	}
	int len = n - nx[n];
	if(n % len) printf("1\n");
	else printf("%d\n", n / len);
}

int main() {
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
  int t, ca = 0;
	while(~scanf("%s", s), s[0] != '.') {
	  getnx();
	}
  return 0;
}

Seek the Name, Seek the Fame

Seek the Name, Seek the Fame
考察kmp性质

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second

const int N = 1e6+5;
char s[N];
int nx[N];
void getnx() {
	int n = strlen(s);
  int k = 0; nx[1] = 0;
	for(int i = 1; i < n; ++i) {
	 while(k && s[i] != s[k]) k = nx[k];
		if(s[i] == s[k]) k++;
		nx[i+1] = k;
	}
	vector<int> ans;
	ans.push_back(n);
  while(k) {
	  ans.push_back(k);
	  k = nx[k];
  }
  for(int i = ans.size()-1; i >= 0; i--) {
    printf("%d%c", ans[i], " \n"[i==0]);
  }
}

int main() {
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
  int t, ca = 0;
	while(~scanf("%s", s)) {
	  getnx();
	}
  return 0;
}

Blue Jeans

Blue Jeans
暴力

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
string s[15];
int main() {
  int t; cin >> t;
  while(t--) {
    int n; cin >> n;
    for(int i = 0; i < n; ++i) cin >> s[i];
    string ans;
    for(int i = 0; i < 57; ++i) {
      for(int j = 3; j + i <= 60; ++j) {
        int f = 0;
        string k = s[0].substr(i, j);
        for(int q = 1; q < n; ++q) {
          if(s[q].find(k) == -1) {
            f = 1;break;
          }
        }
        
        if(!f) {
          if(ans.size() < k.size()) ans = k;
          else if(ans.size() == k.size()) {
            if(ans > k) ans = k;
          }
        }
        
      }
    } 
    
    if(ans.size() < 3) cout << "no significant commonalities\n";
    else cout << ans << "\n";
  }
	return 0;
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页