代码碎片

面试刷题时候,经常使用的STL数据类型...

#include <iostream>
#include <vector>
//#include"parameter.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
#define SOURCE 23
#if SOURCE == 1
int main(int argc, char** argv) {
	int n,m;
	freopen("1.txt", "r", stdin);
	cin >> n;//行数
	cin >> m;//列数
	//vector<vector <int> > array(n ,vector<int>(m));
	vector<vector <int> > array;
	for(int i = 0;i<n;i++)
	{
		vector<int > temp;
		for(int j = 0;j<m;j++)
		{
			int a;
			cin >> a;
			//array[i][j] = a;
			temp.push_back(a);
		}
		array.push_back(temp);
		//temp.clear();
	}


	for(int i = m-1;i>= 0;i--)
	{
		for(int j = 0;j<n;j++)
		{
			cout << array[j][i]<< " ";
		}
		cout << endl;
	}
	return 0;
}
#elif SOURCE == 2
#include <map>
#include <algorithm>
bool cmp(const pair<int,int>& x, const pair<int,int>& y)//false:调换位置
{
	if (x.second <  y.second)
		return false;
	else if ((x.second == y.second) && (x.first > y.first))
		return false;
	else
		return true;
}
int main(int argc, char** argv) {
	int n;
	freopen("2.txt", "r", stdin);
	cin >> n;
	//vector<vector <int> > array(n ,vector<int>(m));
	map<int,int> m;
	for(int i = 0;i<n;i++)
	{
		int index;
		cin >> index;
		m[index]++;
	}

	vector<pair<int,int> > vmap;
	for(map<int,int>::iterator it = m.begin();it != m.end();it++)
	{
		vmap.push_back(make_pair(it->first,it->second));
	}
	sort(vmap.begin(),vmap.end(),cmp);
	for(vector<pair<int,int> >::iterator it = vmap.begin();it != vmap.end();it++)
	{
		cout << it->first << " " << it->second <<endl;
	}
	return 0;
}
#elif SOURCE == 3
/*
#include <map>
#include <algorithm>
bool Runyear(int year)
{
	if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
		return true;
	else
		return false;
}
int F2Date(int a, int b, int c, int y1)
{//1850年1月1日是星期二
 //a月第b个星期c
	int monthDay[13] = {0,31,0,31,30,31,30,31,31,30,31,30,31}; //debug
	int year = 1850;
	int dayNum = 0;
	while(year != y1)
	{
		if(Runyear(year))
			dayNum += 366;
		else
			dayNum += 365;
		year ++;
	}
	for(int i = 1;i< a;i++)
		{
			if((i == 2 )&& (Runyear(y1)))
				dayNum += 29;
			else if(i == 2 )
				dayNum += 28;
			else//else 应该是大多数的情况;
				dayNum += monthDay[i];
		}
	int xingqi =( dayNum %7) + 1;//计算到 (a-1)月末时星期几
	int restDay  = (b-1)*7 + c;
	if(xingqi != 7)
		restDay -= xingqi;

	if((a == 2 )&& (Runyear(y1))&&(restDay > 29))
		restDay = -1;
	else if((a == 2 )&& (!Runyear(y1))&&(restDay > 28))
		restDay = -1;
	else if((a != 2 )&&(restDay > monthDay[a]))
		restDay = -1;
	return restDay;
}
int main(int argc, char** argv) {
	int a, b, c, y1, y2;
	freopen("3.txt", "r", stdin);
	cin >> a;
	cin >> b;
	cin >> c;
	cin >> y1;
	cin >> y2;

	for (int i = y1;i <= y2; i++)
	{
		int day = F2Date(a, b, c, i);

		if (day > 0)//((a >= 1)&&(a<=12)&&(b >= 1)&&(b<=5)&&(i >= 1850)&&(i<=2050)&&(day >= 0))
		{
			char str[100];
			sprintf(str,"%04d/%02d/%02d",i,a,day);// \\反斜杆需要两个来 消除转义
			cout<<str<<endl;
		}
		else
		{
			cout<<"none"<<endl;
		}
	}
	return 0;
}*/
#elif SOURCE == 4
#include <cstdio>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
const int N = 5005;
struct GP
{
	bool buy;
	double price;
	int tot;
	void read(bool b)
	{
		buy = b;
		scanf("%lf%d", &price, &tot);
	}
}gp[N];
map<double, long long> buy, sell;
set<double> price;
int main()
{
	char op[10];
	freopen("4.txt", "r", stdin);
	for (int line = 1; ~scanf("%s", op); line++)
	{
		if (op[0] == 'c')
		{
			int x;
			scanf("%d", &x);
			(gp[x].buy ? buy : sell)[gp[x].price] -= gp[x].tot;
		}
		else
		{
			gp[line].read(op[0] == 'b');
			price.insert(gp[line].price);
			(gp[line].buy ? buy : sell)[gp[line].price] += gp[line].tot;
		}
	}
	long long bsum = 0;
	for (map<double, long long>::iterator i = buy.begin(); i != buy.end(); i++)bsum += i->second;
	long long ssum = 0;
	double ansprice = 0;
	long long anstot = 0;
	for (set<double>::iterator i = price.begin(); i != price.end(); i++)
	{
		if (!(sell[*i] || buy[*i]))continue;
		ssum += sell[*i];
		long long x = min(ssum, bsum);//在当前价格下,成交量应该等于合法买入成交量或者可以合法卖出成交量中的最小值。
		if (x >= anstot)
		{
			anstot = x;
			ansprice = *i;
		}
		bsum -= buy[*i];
	}
	printf("%.2f %lld\n", ansprice, anstot);
	return 0;
}
#elif SOURCE == 5
#include <cstdio>

using namespace std;
const int maxn = 1007;
const int inf = 0x3f3f3f3;
int n, m;
struct node
{
	int to, cost;
};
bool vis[maxn];
int d[maxn];
vector<node> G[maxn];
void init()
{
	for (int i = 0; i<maxn; i++)
	{
		G[i].clear();
	}
}
int prim()
{
	int ans = 0;
	for (int i = 1; i<maxn; i++)
	{
		d[i] = inf;
		vis[i] = 0;
	}
	d[1] = 0;
	while (1){
		int v = -1;
		for (int i = 1; i <= n; i++)
		{
			if (!vis[i] && (v == -1 || d[i]<d[v])) v = i;
		}
		if (v == -1) break;
		vis[v] = 1;
		ans += d[v];
		for (int i = 0; i<G[v].size(); i++)
		{
			if (vis[G[v][i].to]) continue;
			d[G[v][i].to] = min(d[G[v][i].to], G[v][i].cost);
		}
	}
	return ans;
}
int main()
{
	freopen("5.txt","r",stdin);
	int a, b, c; node n1;
	while (~scanf("%d%d", &n, &m)){
		init();
		for (int i = 0; i<m; i++)
		{
			scanf("%d%d%d", &a, &b, &c);
			n1.to = b; n1.cost = c;
			G[a].push_back(n1);
			n1.to = a;
			G[b].push_back(n1);
		}
		int ans = prim();
		printf("%d\n", ans);
	}
}
#elif SOURCE == 6
#include <cstdio>
#include<mem.h>

using namespace std;
int main()
{
    //freopen("6.txt","r",stdin);
    int ans=0;
    int num[10007];
    int n,a;
    while(~scanf("%d",&n)){
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            num[a]++;
        }
    for(int i=1;i<=9999;i++)
    {
        ans+=min(num[i],num[i+1]);
    }
    cout<<ans<<endl;
    }
    return 0;
}
#elif SOURCE == 7
#include<iostream>
using namespace std;
void input(int x1, int y1, int x2, int y2,int a[100][100])    //给数组赋值(染色)
{
    for (int i = x1; i < x2; i++)
    {
        for (int j = y1; j < y2; j++)
            a[i][j]++;
    }
}

int main()
{
    static int a[100][100];
    int n, x1, y1, x2, y2;
    //freopen("7.txt","r",stdin);
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        input(x1, y1, x2, y2, a);
    }
    n = 0;
    //检索不为0的数目
    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 100; j++)
        {
            if (a[i][j] != 0)
                n++;
        }
    }
    cout << n << endl;
    //system("pause");
    return 0;
}
#elif SOURCE == 8
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn=1107;
int mat[maxn][maxn];
int ans[maxn*maxn];
bool vis[maxn][maxn];
int n;
void solve()
{
    memset(vis,0,sizeof(vis));
    int i=0,j=0,num=0;bool first=1,flag=0,point=1;
    while(1){
        ans[num++]=mat[i][j];
        vis[i][j]=1;
        if(i==n-1&&j==n-1) break;
        if(i==n-1&&j==0){
            if(vis[n-2][1]){i=n-1;j=1;point=0;first=0;continue;}
            else{i=n-2;j=1;point=0;first=0;continue;}
        }
        else if(j==n-1&&i==0){
            if(vis[1][n-2]){i=1;j=n-1;first=0;point=1;continue;}
            else {i=1;j=n-2;point=1;first=0;continue;}
        }
        if(i==0&&first){
            first=0;
            j=j+1;
            point=1;
        }
        else if(j==0&&first){
            i=i+1;
            point=0;
            first=0;
        }
        else if(i==n-1&&first){
            j=j+1;
            point=0;
            first=0;
        }
        else if(j==n-1&&first){
            i=i+1;
            point=1;first=0;
        }
        else if(point){
            i=i+1;;j=j-1;
            first=1;
        }
        else{
            first=1;
            i=i-1;j=j+1;
        }
    }
}
int main()
{
   freopen("7.txt","r",stdin);
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
        {
            scanf("%d",&mat[i][j]);
        }
        solve();
        bool first=1;
        for(int i=0;i<n*n;i++)
        {
            if(first){
                 printf("%d",ans[i]);
                 first=0;
            }
            else printf(" %d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}
#elif SOURCE == 9
#include<iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <cctype>
using namespace std;
string s1,s2;
int n;
void solve(string &s)
{
    int l=s.size();
    for(int i=0;i<l;i++)
    {
        if(s[i]>='A'&&s[i]<='Z') s[i]=s[i]-'A'+'a';
    }
}
int main()
{
   freopen("7.txt","r",stdin);
    int flag;
    while(cin>>s1){
        scanf("%d",&flag);
        if(flag){
             scanf("%d",&n);
             for(int i=0;i<n;i++)
             {
                 cin>>s2;
                 if(s2.find(s1)!= s2.npos) cout<<s2<<endl;
             }
        }
        else{
            solve(s1);
            scanf("%d",&n);
             for(int i=0;i<n;i++)
             {
                 cin>>s2;
                  string s3=s2;
                 solve(s2);

                 if(s2.find(s1) != s2.npos) cout<<s3<<endl;
             }
        }
    }
    return 0;
}
#elif SOURCE == 10
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;

struct State
{
	int x, y;
	int layer;
	State(int _x, int _y, int _l = 0) :x(_x), y(_y), layer(_l) {}
	State()
	{
		x = y = layer = 0;
	}
};

int data[1000 + 5][1000 + 5] = { 0 };
//0 0/1 vis 1 0/1 client other clients needs;
std::queue<State> que;
int n, m, k, d, x, y, z;
long long ans;
void bfs();
int main()
{
	freopen("7.txt", "r", stdin);
	scanf("%d%d%d%d", &n, &m, &k, &d);
	//分别表示方格图的大小、
	for (int i = 0; i<m; i++) //栋栋的分店数量
	{
		scanf("%d%d", &x, &y);
		que.push(State(x, y));
	}
	for (int i = 0; i<k; i++) //客户的数量
	{
		scanf("%d%d%d", &x, &y, &z);
		data[x][y] = (data[x][y] | 2) + (z << 2);
	}
	for (int i = 0; i<d; i++) //不能经过的点的数量
	{
		scanf("%d%d", &x, &y);
		data[x][y] |= 1;
	}
	bfs();
	return 0;
}
void bfs()
{
	ans = 0;
	State tmp, cur;
	while (!que.empty()) {//根据店扩展到顾客,每个店同步拓展,因为店是根节点。
		cur = que.front();
		que.pop();
		int curx = cur.x, cury = cur.y, curl = cur.layer;//layer路径长度
		if (curx>0 && cury>0 && curx <= n&&cury <= n&&!(data[curx][cury] & 1))
		{
			data[curx][cury] |= 1;//根据店扩展到顾客,防止走过的路
			que.push(State(curx + 1, cury, curl + 1));
			que.push(State(curx, cury + 1, curl + 1));
			que.push(State(curx - 1, cury, curl + 1));
			que.push(State(curx, cury - 1, curl + 1));
			if (data[curx][cury] & 2) ans += (data[curx][cury] >> 2)*curl;
		}
	}
	cout << ans;
	//printf("%d",ans);
}
#elif SOURCE == 11
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
char str[20];
int num[20];
int main()
{
	freopen("7.txt","r",stdin);
	int ans;
	ans = 0;
	scanf("%s", str);
	num[1] = str[0] - '0';
	num[2] = str[2] - '0';
	num[3] = str[3] - '0';
	num[4] = str[4] - '0';
	num[5] = str[6] - '0';
	num[6] = str[7] - '0';
	num[7] = str[8] - '0';
	num[8] = str[9] - '0';
	num[9] = str[10] - '0';
	for (int i = 1; i <= 9; i++) ans = (ans + num[i] * i) % 11;
	int l = strlen(str);
	if (ans<10 && ans == str[l - 1] - '0')
	{
		printf("Right\n");
	}
	else if (ans == 10 && str[strlen(str) - 1] == 'X'){
		printf("Right\n");
	}
	else
	{
		if (ans<10) printf("%d-%d%d%d-%d%d%d%d%d-%d\n", num[1], num[2], num[3], num[4], num[5], num[6], num[7], num[8], num[9], ans);
		else printf("%d-%d%d%d-%d%d%d%d%d-X\n", num[1], num[2], num[3], num[4], num[5], num[6], num[7], num[8], num[9]);
	}
	return 0;
}
#elif SOURCE == 12
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

int a[1001];

int main(){

	int n, max = 0;
	freopen("5.txt", "r", stdin);
	cin >> n;
	for (int i = 0; i<n; i++)
	{
		cin >> a[i];
	}
	max = a[0];
	for (int i = 1; i<n; i++){//利用了一个常识
		int now = a[i];//而最终矩形一定是连续的几个内部的,所以可以先定一个高度然后向两边拓展。
		//往前走
		for (int j = i - 1; j >= 0; j--){
			if (a[i] <= a[j])
				now += a[i];
			else
				break;
		}
		//往后走
		for (int j = i + 1; j<n; j++){
			if (a[i] <= a[j])
				now += a[i];
			else
				break;
		}
		if (now>max){
			max = now;
		}
	}

	cout << max;

	getchar();
	getchar();
	return 0;
}
#elif SOURCE == 13
//TO DO
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

#define LL long long
#define clr(s,x) memset(s,x,sizeof(s))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const double PI = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 1e3 + 10;

int day[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
bool isRunNian(int y){
	if (y % 400 == 0) return true;
	if ((y % 4 == 0) && (y % 100 != 0)) return true;
	return false;
}
int main(){
	//1850年1月1日是星期二
	//a月第b个星期c
	int y1, y2, a, b, c;
	freopen("5.txt", "r", stdin);
	cin >> a >> b >> c >> y1 >> y2;
	if (c == 7)c = 0;
	int l = min(y1, y2), r = max(y1, y2);
	int d = 0, nw = 2;
	for (int i = 1850; i<l; i++){
		if (isRunNian(i))d += 366;
		else d += 365;
	}
	nw = (nw + d) % 7;//第d+1天星期几
	for (int i = l; i <= r; i++){
		for (int j = 1; j <= 12; j++){
			if (j == a){
				int cnt = 0;
				bool fg = false;
				for (int k = 1; k <= day[isRunNian(i)][j]; k++){
					if (nw == c && (!fg)) cnt++;
					if (cnt == b && (!fg)){
						printf("%d/%02d/%02d\n", i, j, k);
						fg = true;
					}
					if (k == day[isRunNian(i)][j] && cnt < b) cout << "none" << endl;//月末了还未达到第b个星期,则报错;
					nw = (nw + 1) % 7;
				}
			}
			else{
				nw += day[isRunNian(i)][j];
				nw %= 7;
			}
		}
	}
	return 0;
}
#elif SOURCE == 14
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn = 1007;
int num[maxn];
int main()
{
	freopen("5.txt","r",stdin);
	int n, a; int ans = 0;
	scanf("%d", &n);
	memset(num, 0, sizeof(num));
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		num[a + 500]++;
	}
	for (int i = 1; i <= 500; i++)
	{
		ans += min(num[i + 500], num[500 - i]);
	}
	printf("%d\n", ans);
	return 0;
}
#elif SOURCE == 15
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

typedef struct node{
	int x1, y1, x2, y2;
}Node;
Node node[32];                //窗口结构体数组
int a[32];                    //保存各个窗口的上下关系

int main(){
	freopen("5.txt", "r", stdin);
	int n, m, x, y;
	while (scanf("%d%d", &n, &m) != EOF){
		memset(a, 0, sizeof(a));
		memset(node, 0, sizeof(node));
		for (int i = 1; i <= n; i++)
			scanf("%d%d%d%d", &node[i].x1, &node[i].y1, &node[i].x2, &node[i].y2);
		for (int i = 1; i <= n; i++) a[i] = n - i + 1;
		for (int i = 0; i<m; i++){
			scanf("%d%d", &x, &y);
			int flag = 0;
			for (int j = 1; j <= n; j++){
				int foo = a[j];
				if (x >= node[foo].x1 && x <= node[foo].x2 && y >= node[foo].y1 && y <= node[foo].y2){      //如果点击的位置落在该窗口
					printf("%d\n", foo);
					flag = 1;
					for (int k = j; k >= 2; k--) a[k] = a[k - 1];            //调整各窗口相对上下位置
					a[1] = foo;
					break;
				}
			}
			if (!flag) puts("IGNORED");
		}
	}
	return 0;
}
#elif SOURCE == 16
//TODO 命令行选项
#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

int main() {
	string s;
	freopen("5.txt", "r", stdin);
	cin >> s;
	char buffer[260];
	int n;
	cin >> n;
	cin.getline(buffer, 260);     //读取无用字符到这行末
	for (int i = 1; i <= n; i++)
	{
		cin.getline(buffer, 260);
		string a(buffer);     //把char数组转换为string
		vector<string> v;     //申明向量存放切割的string
		map<string, string> map1;     //声明map存放命令和参数,first为命令,second为参数,注意不要和map重名,小心使用不了迭代器
		for (int t = (int)a.find(" "); t != -1; t = (int)a.find(" "))
		{
			string temp = a.substr(0, t);
			v.push_back(temp);
			a = a.substr(t + 1);
		}
		v.push_back(a);     //上面这部分切割了字符串并全部放入向量。

		for (int j = 0; j<v.size(); j++)     //对向量里面元素遍历
		{
			if (v[j].size() == 2 && v[j][0] == '-')     //如果是命令
			{
				char c = v[j][1];     //找到命令
				int next = (int)s.find(c);
				if (next == -1)     //如果命令非法则跳出
					break;
				if (map1.find(v[j]) == map1.end())     //如果命令不存在则添加,键值对第二个参数默认为空
					map1.insert(pair<string, string>(v[j], ""));
				if (next + 1<s.size() && s[next + 1] == ':' && j + 1<v.size()){
					map<string, string>::iterator it = map1.find(v[j]);
					it->second = v[j + 1];
					j++;
				}     //上面是对第i+1个v元素检查,如果i个v元素需要参数则写入map键值对的second,此办法直接覆盖前一个参数
			}
			else if (j != 0)     //如果不是命令且不为第一个(程序名)则跳出
				break;

		}
		//由于map本身就是按键的值升序排列,少去了自己排序麻烦,可直接迭代器输出
		cout << "Case " << i << ":";
		for (map<string, string>::iterator it = map1.begin(); it != map1.end(); it++)
		{
			cout << " " << it->first;
			if (it->second != "")     //这么写是为了输出格式考虑,以免程序不通过
				cout << " " << it->second;
		}
		cout << endl;
	}
	return 0;
}
#elif SOURCE == 17//freopen("5.txt", "r", stdin);
//1每个数等于它上方两数之和。
//2每行数字左右对称,由1开始逐渐变大。
//3第n行的数字有n项。
//4第n行数字和为2n-1。
//5第n行的m个数可表示为 C(n-1,m-1),即为从n-1个不同元素中取m-1个元素的组合数。
//6第n行的第m个数和第n-m+1个数相等 ,为组合数性质之一。
//7每个数字等于上一行的左右两个数字之和。可用此性质写出整个杨辉三角。
//	即第n+1行的第i个数等于第n行的第i-1个数和第i个数之和,这也是组合数的性质之一。
//	即 C(n+1,i)=C(n,i)+C(n,i-1)。
//8(a+b)n的展开式中的各项系数依次对应杨辉三角的第(n+1)行中的每一项。
//9将第2n+1行第1个数,跟第2n+2行第3个数、第2n+3行第5个数……连成一线,这些数的和是第4n+1个斐波那契数;
//	将第2n行第2个数(n>1),跟第2n-1行第4个数、第2n-2行第6个数……这些数之和是第4n-2个斐波那契数。
//10将各行数字相排列,可得11的n-1(n为行数)次方:1=11^0; 11=11^1; 121=11^2……当n≥5时会不符合这一条性质,
//	此时应把第n行的最右面的数字"1"放在个位,然后把左面的一个数字的个位对齐到十位... ...,以此类推,
//	把空位用“0”补齐,然后把所有的数加起来,得到的数正好是11的n-1次方。以n=11为例,第十一行的数为:
//	1,10,45,120,210,252,210,120,45,10,1,结果为 25937424601=1110。
#include <stdio.h>
#define N 1001
#define C(n,m) c[(n)+1][(m)+1]
#define MOD 1000000007
int c[N][N];
int main()
{
	int n;
	freopen("5.txt", "r", stdin);
	scanf("%d", &n);
	c[0][0] = 1;
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= i; j++)
		c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;
	int ans = 0;
	for (int i = 2; i <= n - 2; i++)
		ans = (ans + (long long)C(n - 1, i)*((i - 1)*(n - i - 1)) % MOD) % MOD;
	printf("%d\n", ans);
	return 0;
}
//http://erona.me/2014/12/05/CCF%E6%A8%A1%E6%8B%9F%E9%A2%98-%E6%9C%89%E8%B6%A3%E7%9A%84%E6%95%B0/
#elif SOURCE == 18//freopen("5.txt", "r", stdin);
//TODO  I'm stuck!
#include<cstdio>
using namespace std;
char GetChar()
{
	char c;
	do{
		c = getchar();
	} while (c == '\n' || c == ' ');
	return c;
}
#define N 60
char maps[N][N];
int r, c;
bool vis1[N][N];
bool vis2[N][N];
int sx, sy;
int tx, ty;
void dfs1(int x, int y)
{
	if (x <= 0 || x>c || y <= 0 || y>r)return;
	char c = maps[y][x];
	if (c == '#')return;
	if (vis1[x][y])return;
	vis1[x][y] = true;


	if (c == '+' || c == '-')
	{
		dfs1(x + 1, y);
		dfs1(x - 1, y);
	}
	if (c == '+' || c == '|')
	{
		dfs1(x, y + 1);
		dfs1(x, y - 1);
	}
	if (c == '.')dfs1(x, y + 1);
}
void dfs2(int x, int y)
{
	if (x <= 0 || x>c || y <= 0 || y>r)return;
	if (vis2[x][y])return;
	vis2[x][y] = true;
	if (maps[y][x - 1] == '-' || maps[y][x - 1] == '+')dfs2(x - 1, y);
	if (maps[y][x + 1] == '-' || maps[y][x + 1] == '+')dfs2(x + 1, y);
	if (maps[y - 1][x] == '|' || maps[y - 1][x] == '+' || maps[y - 1][x] == '.')dfs2(x, y - 1);
	if (maps[y + 1][x] == '|' || maps[y + 1][x] == '+')dfs2(x, y + 1);

}
int main()
{
	freopen("5.txt", "r", stdin);
	scanf("%d%d", &r, &c);
	for (int i = 1; i <= r; i++)
	{
		for (int j = 1; j <= c; j++)
		{
			maps[i][j] = GetChar();
			if (maps[i][j] == 'S')
			{
				sx = j;
				sy = i;
				maps[i][j] = '+';
			}
			else if (maps[i][j] == 'T')
			{
				tx = j;
				ty = i;
				maps[i][j] = '+';
			}
		}
	}
	dfs1(sx, sy);
	if (!vis1[tx][ty])
	{
		printf("I'm stuck!\n");
		return 0;
	}
	dfs2(tx, ty);
	int ans = 0;
	for (int i = 1; i <= r; i++)
	{
		for (int j = 1; j <= c; j++)
		{
			if (vis1[j][i] && !vis2[j][i])
			{
				ans++;
				//printf("%d,%d--%c\n",j,i,maps[i][j]);
			}
		}
	}

	printf("%d\n", ans);
	return 0;
}
#elif SOURCE == 19 //TODO 网络延时
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define N 200010
#define M 10*N

struct Edge{
    int to,next;
}edge[M];
int tot;
int head[N];

int n,m;
int dp[N][2];

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void dfs(int u)
{
    dp[u][0]=dp[u][1]=0;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        dfs(v);
        if(dp[v][0]+1>=dp[u][0]){
            dp[u][1]=dp[u][0];
            dp[u][0]=dp[v][0]+1;
        }
        else if(dp[v][0]+1>dp[u][1])
            dp[u][1]=dp[v][0]+1;
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        for(int i=2;i<=n;i++){
            int x;
            scanf("%d",&x);
            add(x,i);
        }
        for(int i=1;i<=m;i++){
            int x;
            scanf("%d",&x);
            add(x,i+n);
        }
        dfs(1);
        int ans=0;
        for(int i=1;i<=n+m;i++) ans=max(ans,dp[i][0]+dp[i][1]);
        printf("%d\n",ans);
    }
    return 0;
}
#elif SOURCE == 20 //TODO 货物调度
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <deque>
using namespace std;
#define CLR(a,v) memset(a,v,sizeof(a))
const int N=105*7;
const int M=(505*7*2+N*3)*2;
const int INF=0x3f3f3f3f;
struct Edge
{
    int v,w,c,next;
}edge[M];
int ehead[N];
int ecnt;
inline void addedge(int u,int v,int w,int c)
{
    edge[ecnt]={v,w,c,ehead[u]};
    ehead[u]=ecnt++;
}
inline void AddEdge(int u,int v,int w,int c)
{
    //printf("%d->%d(%d,%d)\n",u,v,w,c);
    addedge(u,v,w,c);
    addedge(v,u,0,-c);
}
int s,t;
int pre[N],pree[N];
int dist[N];
bool inq[N];
bool spfa()
{
    CLR(inq,false);
    CLR(dist,INF);
    CLR(pre,-1);
    dist[s]=0;
    //queue<int> q;
    deque<int> q;
    //q.push(s);
    q.push_back(s);
    while(q.size())
    {
        //int u=q.front();q.pop();
        int u=q.front();q.pop_front();
        inq[u]=false;
        for(int i=ehead[u];~i;i=edge[i].next)
        {
            if(edge[i].w)
            {
                int v=edge[i].v;
                int ndist=dist[u]+edge[i].c;
                if(ndist<dist[v])
                {
                    dist[v]=ndist;
                    pre[v]=u;
                    pree[v]=i;
                    if(!inq[v])
                    {
                        //q.push(v);
                        if(q.size()&&dist[v]<dist[q.front()])
                            q.push_front(v);
                        else
                            q.push_back(v);
                        inq[v]=true;
                    }
                }
            }
        }
    }
    return dist[t]!=INF;
}
int mcmf(int _s,int _t)
{
    s=_s;t=_t;
    int cost=0;
    while(spfa())
    {
        int f=INF;
        for(int u=t;~pre[u];u=pre[u])f=min(f,edge[pree[u]].w);
        for(int u=t;~pre[u];u=pre[u])
        {
            edge[pree[u]].w-=f;
            edge[pree[u]^1].w+=f;
        }
        cost+=dist[t]*f;
    }
    return cost;
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        CLR(ehead,-1);ecnt=0;
        int s=n*7,t=s+1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<7;j++)
            {
                int a;
                scanf("%d",&a);
                AddEdge(s,j*n+i,a,0);//源点连向每个城市每天
            }
            for(int j=0;j<7;j++)
            {
                int b;
                scanf("%d",&b);
                AddEdge(j*n+i,t,b,0);//每城市每天连向汇点
            }
            int v,w;
            scanf("%d%d",&v,&w);
            for(int j=0;j<7;j++)AddEdge(j*n+i,(j+1)%7*n+i,v,w); //连向下一天,费用为存储费用
        }
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            u--;v--;
            //城市之间的无向边,每天都连一遍
            for(int i=0;i<7;i++)
            {
                AddEdge(i*n+u,i*n+v,INF,w);
                AddEdge(i*n+v,i*n+u,INF,w);
            }
        }
        printf("%d\n",mcmf(s,t));
    }
    return 0;
}
#elif SOURCE == 21 //TODO无线网络
/*目前在一个很大的平面房间里有 n 个无线路由器,每个无线路由器都固定在某个点上。
任何两个无线路由器只要距离不超过 r 就能互相建立网络连接。
除此以外,另有 m 个可以摆放无线路由器的位置。
你可以在这些位置中选择至多 k 个增设新的路由器。
你的目标是使得第 1 个路由器和第 2 个路由器之间的网络连接经过尽量少的中转路由器。
请问在最优方案下中转路由器的最少个数是多少?*/
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 300;
struct point {
    int x,y,isadded;
} p[maxn*maxn];
int n,m,k,d[maxn];
double r;
double getDis(int i,int j) {
    double tmp = (LL)(p[i].x - p[j].x)*(p[i].x - p[j].x);
    tmp += (LL)(p[i].y - p[j].y)*(p[i].y - p[j].y);
    return sqrt(tmp);
}
struct node {
    int id,pass,added;
    node(int x = 0,int y = 0,int z = 0) {
        id = x;
        pass = y;
        added = z;
    }
};
queue<node>q;
int bfs(){
    memset(d,0x3f,sizeof(d));
    while(!q.empty()) q.pop();
    d[0] = 0;
    q.push(node(0,0,0));
    while(!q.empty()){
        node now = q.front();
        q.pop();
        for(int i = 1; i < n+m; ++i)
        if(i != now.id && getDis(i,now.id) <= r&& now.added+p[i].isadded <= k && d[i] > now.pass+1){
            d[i] = now.pass + 1;
            //cout<<i<<" "<<d[i]<<endl;
            q.push(node(i,d[i],now.added+p[i].isadded));
        }
    }
    return d[1] - 1;
}
int main() {
    while(~scanf("%d %d %d %lf",&n,&m,&k,&r)) {
        for(int i = 0; i < n + m; ++i) {
            scanf("%d %d",&p[i].x,&p[i].y);
            p[i].isadded = i >= n?1:0;
        }
        cout<<bfs()<<endl;
    }
    return 0;
}
#elif SOURCE == 21 //
#elif SOURCE == 23 //
#include <stdio.h>
int P[100][100] = {0};
int B2T_P(int m,int n)
{
    if(((m >= 1)&&(n == 0)) || ((n > m)&& ( m>= 0)))
        return 0;
    if(((m == 0)&&(n == 0)) || ((n == 1)&& ( m>= 1)) || ((m == n)&& (n >= 1)))
        return 1;
    for(int i = 1; i <= n; ++i)
    {
        P[i][0] = 0;
    }
    for(int i = 0; i <= n || i <= m; ++i)
    {
        for(int j = 0; j < i; ++j)
        {
            P[j][i] = 0;
        }
    }
    P[0][0] = 1;
    for(int i = 1; i <= m; ++i)
    {
        P[i][1] = 1;
    }
    for(int i = 1; i <= m && i <= n; ++i)
    {
        P[i][i] = 1;
    }
    for(int i = 3;i <= m;i++)
    {
        for(int j = 2;j<= m-1;j++)
        {
            P[i][j] = P[i - 1][j - 1] + P[i - j][j];
        }
    }
    return P[m][n];
}
int T2B_P(int m,int n)
{
    if(((m >= 1)&&(n == 0)) || ((n > m)&& ( m>= 0)))
        return 0;
    if(((m == 0)&&(n == 0)) || ((n == 1)&& ( m>= 1)) || ((m == n)&& (n >= 1)))
        return 1;
    return (T2B_P(m-1,n-1) + T2B_P(m-n,n));
}
void print(int m, int n)
{
    for(int i = 0; i < m; ++i)
    {
        for(int j = 0; j < n; ++j)
            printf("%d ", P[i][j]);
        printf("\n");
    }
}
int main()
{
    int a = B2T_P(7,4);
    print(7,4);
    int b = T2B_P(7,4);
    printf("\n\n");
    printf("B2T_P: %d   T2B_P = %d\n",a,b);
    return 0;
}
#elif SOURCE == 22 //
#include <iostream>
#include <vector>
using namespace std;

void change(int &a, int &b)
{
 int temp;
 temp = a;
 a = b;
 b = temp;
}
void quickSort(int* a, int l, int u)
{
 int i, m;
 if (l >= u) return;
 m = l;
 for (i = l + 1; i <= u; i++)
  if (a[i] > a[l])
   change(a[++m], a[i]);
 change(a[l], a[m]);
 quickSort(a, l, m - 1);
 quickSort(a, m + 1, u);
}
int main()
{
 vector<int> result;
 vector<int> ra;
 vector<int> rb;
 int num;
 int* a;
 int* b;
 while (cin>>num)
 {
  if (num == 0)
   break;
  a = new int[num];
  b = new int[num];
  for (int i = 0; i < num; i++)
   cin>>a[i];
  for (int i = 0; i < num; i++)
   cin>>b[i];
  quickSort(a, 0, num-1);
  quickSort(b, 0, num-1);
  int win = 0;
  int fail = 0;
  int draw = 0;
  int ib = 0, jb = 0;
  int ie = num - 1, je = num - 1;
  while (ib <= ie)
  {
   if (a[ie] > b[je])
   {
    win++;
    ie--;
    je--;
   }else if (a[ie] < b[je])
   {
    fail++;
    ie--;
    jb++;
   }else
   {
    if (a[ib] > b[jb])
    {
     win++;
     ib++;
     jb++;
    }else
    {
     if (a[ie] < b[jb])
      fail++;
     ie--;
     jb++;
    }
   }
  }
  result.push_back(200*(win - fail));

 }
 for (size_t i = 0; i != result.size(); i++)
 {
  cout<<result[i]<<endl;
 }
 return 0;
}
#elif SOURCE == 23
// string::find
#include <iostream>       // std::cout
#include <string>         // std::string

int main ()
{
  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");

  // different member versions of find in the same order as above:
  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';

  found=str.find("needles are small",found+1,6);
  if (found!=std::string::npos)
    std::cout << "second 'needle' found at: " << found << '\n';

  found=str.find("haystack");
  if (found!=std::string::npos)
    std::cout << "'haystack' also found at: " << found << '\n';

  found=str.find('.');
  if (found!=std::string::npos)
    std::cout << "Period found at: " << found << '\n';

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  std::cout << str << '\n';

  return 0;
}
#elif SOURCE == 24

// string::find
#include <iostream>       // std::cout
#include <string>         // std::string

using namespace std;
int main()
{
while (true)
	{
		string str;// ("There are two needles in this haystack with needles.");
		getline(cin, str);
		if (str.empty())
			break;
		// different member versions of find in the same order as above:
		int length = str.size();
		int output = 0;
		while (length--)
			{
				output++;
				if (str[length] != ' ')
				break;
			}
		cout << output<< endl;
		str.erase();
	}

return 0;
}

#include<map>  
#include<string>  
#include<iostream>  
using namespace std;
int main()
{
	while (true)
	{
		int n, nn;// ("There are two needles in this haystack with needles.");
		map<int, int> inputArray;
		cin >> n;
		while (n--)
		{
			int t;
			cin >> t;
			inputArray[t] = 0;
		}
		if (inputArray.empty())
			continue;
		// show content:
		for (std::map<int, int>::iterator it = inputArray.begin(); it != inputArray.end(); ++it)
			std::cout << it->first <<endl;
		//inputArray.erase();
	}

	return 0;
}
#endif


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值