7-1 Sexy Primes
#include <cstdio>
#include <cmath>
using namespace std;
bool isPrime(int n){
if(n<=1) return false;
int sqr=(int)sqrt(1.0*n);
for(int i=2;i<=sqr;i++)
if(n%i==0) return false;
return true;
}
bool isSexyPrime(int n){
if(isPrime(n) && isPrime(n+6)) return true;
else return false;
}
int main(void){
int N;
scanf("%d",&N);
if(isSexyPrime(N-6)){
printf("Yes\n%d",N-6);
}else if(isSexyPrime(N)){
printf("Yes\n%d",N+6);
}else{
printf("No\n");
do{
N++;
}while(!isSexyPrime(N) && !isSexyPrime(N-6));
printf("%d",N);
}
return 0;
}
7-2 Anniversary
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(){
int n, m, cnt = 0;
string ans, minn;
map<string, int>mp;
scanf("%d", &n);
for(int i = 1; i <= n; ++ i){
string temp;
cin >> temp;
mp[temp] = i;
}
scanf("%d", &m);
for(int i = 0; i < m; ++ i){
string temp, tempbirth;
cin >> temp;
tempbirth = temp.substr(6, 8);
if(mp[temp] > 0){
if(cnt ++ == 0 || tempbirth < minn){
minn = tempbirth;
ans = temp;
}
}
if(cnt == 0){
if(i == 0 || tempbirth < minn){
minn = tempbirth;
ans = temp;
}
}
}
printf("%d\n%s",cnt, ans.c_str());
}
7-3 Telefraud Detection
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
using namespace std;
const int INF=0;
const int maxn=1030;
int N,M,K;
int G[maxn][maxn];
vector<int> suspect;
set<int> Set[maxn];
bool vis[maxn];
bool inq[maxn];
bool isSuspect(int i){
int k=0,cnt=0,sum=0;
for(int j=1;j<=N;j++){
if(G[i][j]!=INF && G[i][j]<=5){
k++;
if(G[j][i]!=0) cnt++;
}
}
if(k>K && 1.0*cnt/k<=0.2) return true;
return false;
}
void BFS(int s){
queue<int> q;
q.push(s);
inq[s]=true;
while(!q.empty()){
int u=q.front();
q.pop();
Set[s].insert(u);
for(int i=0;i<=suspect.size();i++){
int v=suspect[i];
if(!inq[v] && G[u][v]!=INF && G[v][u]!=INF){
q.push(v);
inq[u]=true;
}
}
}
}
int main(void){
fill(G[0],G[0]+maxn*maxn,INF);
scanf("%d%d%d",&K,&N,&M);
for(int i=0;i<M;i++){
int caller,receiver,duration;
scanf("%d%d%d",&caller,&receiver,&duration);
G[caller][receiver]+=duration;
}
for(int i=1;i<=N;i++){
if(isSuspect(i)){
suspect.push_back(i);
}
}
if(suspect.size()==0){
printf("None");
return 0;
}
for(int i=0;i<suspect.size();i++){
if(!inq[suspect[i]]){
BFS(suspect[i]);
}
}
for(int i=0;i<suspect.size();i++){
int v=suspect[i];
if(vis[v]) continue;
set<int>::iterator it=Set[v].begin();
printf("%d",*it++);
vis[*it]=true;
for(;it!=Set[v].end();it++){
printf(" %d",*it);
vis[*it]=true;
}
printf("\n");
}
return 0;
}
7-4 Structure of a Binary Tree
#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
TreeNode *left, *right, *parent;
int val;
TreeNode(int v = 0):left(NULL), right(NULL), parent(NULL), val(v){}
};
int pos[32], ino[32];
map<int, TreeNode*> mp;
map<int, int> lev;
bool isfull = true;
TreeNode *build(int posL, int posR, int inoL, int inoR, int depth){
if(posL > posR) return NULL;
int e = pos[posR], idx = inoL;
while(e != ino[idx]) ++idx;
TreeNode *r = new TreeNode(e);
lev[e] = depth;
r->left = build(posL, posL + idx - 1 - inoL, inoL, idx - 1, depth + 1);
r->right = build(posR - 1 -(inoR - idx - 1),posR - 1, idx + 1, inoR, depth + 1);
if(r->left) r->left->parent = r;
if(r->right) r->right->parent = r;
if((!r->left && r->right) || (r->left && !r->right)) isfull = false;
mp[e] = r;
return r;
}
void sol(string &s, TreeNode *r){
if(s.find("root") != string::npos){
int num;
sscanf(s.c_str(), "%d is the root", &num);
r->val == num ? printf("Yes\n"):printf("No\n");
}
else if(s.find("siblings") != string::npos){
int a, b;
sscanf(s.c_str(), "%d and %d are siblings", &a, &b);
TreeNode *p = mp[a], *q = mp[b];
p->parent == q->parent ? printf("Yes\n") : printf("No\n");
}
else if(s.find("parent") != string::npos){
int a, b;
sscanf(s.c_str(), "%d is the parent of %d", &a, &b);
TreeNode *p = mp[a], *q = mp[b];
p == q->parent ? printf("Yes\n") : printf("No\n");
}
else if(s.find("left child") != string::npos){
int a, b;
sscanf(s.c_str(), "%d is the left child of %d", &a, &b);
TreeNode *p = mp[a], *q = mp[b];
p == q->left ? printf("Yes\n") : printf("No\n");
}
else if(s.find("right child") != string::npos){
int a, b;
sscanf(s.c_str(), "%d is the right child of %d", &a, &b);
TreeNode *p = mp[a], *q = mp[b];
p == q->right ? printf("Yes\n") : printf("No\n");
}
else if(s.find("level") != string::npos){
int a, b;
sscanf(s.c_str(), "%d and %d are on the same level", &a, &b);
lev[a] == lev[b] ? printf("Yes\n") : printf("No\n");
}
else if(s.find("full") != string::npos){
isfull ? printf("Yes\n") : printf("No\n");
}
}
int main()
{
int n, m;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", pos + i);
}
for(int i = 0; i < n; ++i){
scanf("%d", ino + i);
}
TreeNode *r = build(0, n - 1, 0, n - 1, 1);
scanf("%d", &m);
getchar();
while(m--){
string s;
getline(cin, s);
sol(s, r);
}
return 0;
}