

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e3+100;
struct edge{
int to,next;
}e[maxx<<1];
int head[maxx<<1];
int n,tot=0;
inline void add(int u,int v)
{
e[tot].next=head[u],e[tot].to=v,head[u]=tot++;
}
inline void dfs(int u,int num,int &_max)
{
_max=max(_max,num);
for(int i=head[u];i!=-1;i=e[i].next)
{
int to=e[i].to;
dfs(to,num+1,_max);
}
}
int main()
{
memset(head,-1,sizeof(head));
scanf("%d",&n);
int x;
queue<int> q;
while(q.size()) q.pop();
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x==0) q.push(i);
else add(x,i);
}
int _max=0;
while(q.size())
{
x=q.front();
q.pop();
dfs(x,1,_max);
}
cout<<_max<<endl;
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<stdio.h>
#include<string.h>
int main()
{
int k=1,i;
char a[10000];
scanf("%s",a);
int fog=0;
for( i=1;i<strlen(a);i++)
{
if(strlen(a)%i!=0)
continue;
for( k=0;k<strlen(a);k++)
{
if(k+i<strlen(a))
if(a[k]!=a[k+i])
{
fog=1;
break;
}
}
if(fog==0)
break;
if(fog==1)
fog=0;
}
printf("%d",strlen(a)/i);
return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 105, mod = 20123;
char str[N];
int get(vector<int> &numbers, int l, int r)
{
vector<int> dividend;
for (int i = r; i >= l; --i) dividend.push_back(numbers[i]);
int t = 0;
for (int i = dividend.size()-1; i >= 0; --i)
{
dividend[i] += t * 10;
t = dividend[i] % mod;
dividend[i] /= mod;
while (dividend.size() && !dividend.back()) dividend.pop_back();
}
return t;
}
int expmod(int a, int b)
{
int res = 1;
while (b)
{
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int dp(int u)
{
int n = strlen(str);
vector<int> numbers;
for (int i = 0; i < n; ++i) numbers.push_back(str[i]-'0');
int res = 0;
for (int i = 0; i < n; ++i)
{
if (i) res = (res + get(numbers, 0, i-1) * expmod(10, n-i-1)) % mod;
if (numbers[i] == u) res = (res + get(numbers, i+1, n-1) + 1) % mod;
else if (numbers[i] > u) res = (res + expmod(10, n-i-1)) % mod;
}
return res;
}
int main()
{
scanf("%s", str);
printf("%d", (dp(1) + dp(2)) % mod);
return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e5+100;
struct node{
string s;
int num;
node(string tt,int a)
{
s=tt,num=a;
}
};
string s;
int n;
inline int bfs()
{
map<string,bool> mp;
queue<node> q;
mp[s]=1;
q.push(node(s,0));
string tt;
while(q.size())
{
node u=q.front();
q.pop();
if(u.s.find("2012")!=-1) return u.num;
tt=u.s;
for(int i=0;i<tt.length()-1;i++)
{
swap(tt[i],tt[i+1]);
if(mp[tt]==0)
{
mp[tt]=1;
q.push(node(tt,u.num+1));
}
swap(tt[i],tt[i+1]);
}
}
return -1;
}
int main()
{
cin>>n>>s;
cout<<bfs()<<endl;
return 0;
}

#include<iostream>
using namespace std;
int t,xj[100], m, n, cnt=0;
void dfs(int x)
{
if (xj[x]!=1)//没有踩到陷阱
{
if (x == n)//一种方案到达终点
cnt++;
else
{
if (x + 1 <= n)
dfs(x + 1);
if (x + 2 <= n)
dfs(x + 2);
}
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> t;
xj[t] = 1;
}
dfs(1);
cout << cnt << endl;
return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int yami[10010];
int f[10010];
int maxx = -2e9;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>yami[i];
f[1] = 1;
for(int i=2;i<=n;i++)
{
f[i] = 1;
for(int j=1;j<i;j++)
if(yami[j]<=yami[i]) f[i] = max(f[i],f[j]+1);
}
for(int i=1;i<=n;i++) maxx = max(maxx,f[i]);
cout<<maxx;
return 0;
}


AC代码
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <functional>
using namespace std;
int n;
int in[26];
int c[26];
queue <int> qu;
vector <int> map[26];
string ans;
void paixu()
{
int ram, q = 0;
int i = 0;
while(q<n) //检查入度
{
if (c[i] == 1)
{
q++;
if (in[i] == 0)
{
qu.push(i);
}
}
i++;
}
while (qu.empty() == 0)//检查队列
{
ram = qu.front();
qu.pop();
ans.push_back((char)ram + 65);
for (int i = 0; i < map[ram].size(); i++)
{
in[map[ram][i]]--;
if (in[map[ram][i]] == 0)
{
qu.push(map[ram][i]);
}
}
}
}
int main()
{
char a, b;
while (scanf("%c%*c%c%*c",&a,&b)!=EOF)
{
if (c[(int)a- 65] == 0)//用于标记出现的顶点和个数
{
c[(int)a - 65] = 1;
n += 1;
}
if (c[(int)b - 65] == 0)
{
c[(int)b - 65] = 1;
n += 1;
}
map[(int)a-65].push_back((int)b-65);//存图
in[(int)b-65]++;//存入度
}
paixu();
if (ans.size() == n)
{
cout << ans;
}
else
{
cout << "No Answer!"<<endl;
}
return 0;
}

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map<string,int>mp;
map<string,string>mmp;
int main(){
string s,name,info;
int type;
while(cin>>s){
if(s == "exit")
break;
else if(s == "create"){
cin>>name>>type>>info;
mp[name] = type;
mmp[name] = info;
}else if(s == "open"){
cin>>name;
while(mp[name] == 1){
name = mmp[name];
}
cout<<mmp[name]<<endl;
}
}
return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
char str[2020],c[10];//有10,还有JQK,不好办。把10变成*
int len=0;
while(scanf("%s",c)!=EOF)
{
if(strlen(c)==2)str[len++]='*';
else str[len++]=c[0];
}
str[len]='\0';
if(len!=52)printf("-1\n");
else
{
char s[2020];
int lens=0,pos=0,temp;//pos当前在牌堆的第几张牌了
while(lens<52)//牌堆洗完之前
{
if(str[pos]=='J')temp=11;
else if(str[pos]=='Q')temp=12;
else if(str[pos]=='K')temp=13;
else if(str[pos]=='*')temp=10;
else temp=str[pos]-'0';
if(temp>=lens)s[lens]=str[pos];
else//否则就移动元素
{
for(int i=lens;i>=temp;--i)s[i+1]=s[i];
s[temp]=str[pos];
}
lens++;
pos++;
}
for(int i=0;i<52;++i)printf(s[i]=='*'?"10 ":"%c ",s[i]);
}
return 0;
}

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
int m,n,fama[30],sum[30],temp,flag;
int DFS(int weight,int num)
{//weight是要称的重量 num是砝码的序号
if(flag || num<0 || abs(weight)>sum[num])//当已经称出 或者越界
return 0;//或者接下来的重量肯定无法称出 就回去 因为没那么多了
if(weight==0 || abs(weight)==sum[num])
{
flag=1;
return 1;
}//选择放在物品对面 或者不放 或者和物品一边
return DFS(weight-fama[num],num-1) || DFS(weight,num-1) || DFS(weight+fama[num],num-1);
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=0;i<n;++i) scanf("%d",&fama[i]);
std::sort(fama,fama+n);
for(int i=0;i<n;++i) sum[i]=i?fama[i]+sum[i-1]:fama[i];//把砝码的前部分和求出 便于剪枝
for(int i=0;i<m;++i)
{
scanf("%d",&temp);
flag=0;
printf("%s\n",DFS(temp,n-1)?"YES":"NO");
}
return 0;
}

被折叠的 条评论
为什么被折叠?



