最小(大)化表示代码如下
#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;char s[100005];intmin_show(){int lens =strlen(s);int i =0, j =1, k =0;//表示从i开始k长度和从j开始k长度的字符串相同while(i < lens && j < lens && k < lens){int cha = s[(i + k)% lens]- s[(j + k)% lens];//t用来计算相对应位置上那个字典序较大if(!cha)k++;//字符相等的情况else{if(cha >0)i += k +1;//i位置大,最大表示法: j += k+1else j += k +1;//j位置大,最大表示法: i += k+1if(i == j)j++;
k =0;}}returnmin(i, j);}intmain(){
ios::sync_with_stdio(false);int n;
cin >> n;for(int i =0; i < n; i++){
cin >> s;
cout <<min_show()<< endl;}return0;}
求连通块
//uva 572 油田#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;constint maxn =200;int m, n;int cnt;char s[maxn][maxn];int a[maxn][maxn];int dx[10]={0,0,1,-1,1,-1,1,-1};int dy[10]={1,-1,0,0,1,-1,-1,1};voiddfs(int x,int y,int cnt){for(int i =0; i <8; i++){int xx = x + dx[i];int yy = y + dy[i];if(xx<1|| x>m || yy<1|| yy>n)continue;if(a[xx][yy]|| s[xx][yy]!='@')continue;
a[xx][yy]= cnt;dfs(xx, yy, cnt);}}intmain(){
ios::sync_with_stdio(false);while(cin >> m >> n){
cnt =0;memset(a,0,sizeof(a));for(int i =1; i <= m; i++){for(int j =1; j <= n; j++){
cin >> s[i][j];}}for(int i =1; i <= m; i++){for(int j =1; j <= n; j++){if(s[i][j]=='@'&& a[i][j]==0){
a[i][j]=++cnt;dfs(i, j, cnt);}}}for(int i =1; i <= m; i++){for(int j =1; j <= n; j++){
cout << a[i][j]<<" ";}cout << endl;}
cout << cnt << endl;}return0;}
拓扑排序
//UVA10305 给任务排序//参考代码:#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;constint maxn =100+10;int n, m, x, y;int in[maxn];
pair<int,int>a[100005];
vector<int>G[maxn];int ans[maxn];int cnt;voidinit(){
cnt =0;memset(ans,0,sizeof(ans));}boolbfs(){
queue<int>q;for(int i =1; i <= n; i++){if(in[i]==0)q.push(i);}while(!q.empty()){int t = q.front();
q.pop();
ans[++cnt]= t;for(int i =0; i < G[t].size(); i++){
in[G[t][i]]--;if(in[G[t][i]]==0)q.push(G[t][i]);}}return cnt == n;}intmain(){
ios::sync_with_stdio(false);while(cin >> n >> m && n){init();memset(in,0,sizeof(in));memset(a,0,sizeof(a));for(int i =1; i <= n; i++)G[i].clear();for(int i =1; i <= m; i++){
cin >> x >> y;
a[i]=make_pair(x, y);}sort(a +1, a +1+ m);int c =unique(a +1, a +1+ m)- a;for(int i =1; i <= c; i++){
G[a[i].first].push_back(a[i].second);
in[a[i].second]++;}if(bfs()){for(int i =1; i <= cnt; i++)cout << ans[i]<<" ";
cout << endl;}}return0;}//写法二:#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;constint maxn =1e5+100;int d, n, m, x, y;int in[maxn];int ans[maxn];int cnt;structnode{int to, nt;}e[maxn];int h[maxn];int num;voidinit(){
cnt =0;
num =0;memset(in,0,sizeof(in));memset(ans,0,sizeof(ans));memset(h,0,sizeof(h));}voidadd(int x,int y){
e[++num].to = y;
e[num].nt = h[x];
h[x]= num;}voidtoposort(){
priority_queue<int>q;for(int i =1; i <= n; i++){if(in[i]==0)q.push(i);}while(!q.empty()){int u = q.top();
ans[++cnt]= u;
q.pop();for(int i = h[u]; i; i = e[i].nt){int v = e[i].to;
in[v]--;if(in[v]==0)q.push(v);}}return;}intmain(){
ios::sync_with_stdio(false);
cin >> d;while(d--){
cin >> n >> m;init();for(int i =1; i <= m; i++){
cin >> x >> y;
in[x]++;add(y, x);}toposort();bool f =0;for(int i =1; i <= n; i++){if(in[i]!=0){
f =1;break;}}if(f)cout <<"Impossible!"<< endl;else{for(int i = cnt; i >=1; i--)cout << ans[i]<<" ";
cout << endl;}}return0;}
蔡锷公式求星期几
#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;constint maxn =1e5+10;
string s[maxn];int n;int t;bool flag;int val[30];int vis[30];boolisyear(int x){if(x %4==0&& x %100!=0|| x %400==0)return1;return0;}//蔡勒公式!//1582年10月4日后://w = (d + 1 + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; (包括当年)//1582年10月4日前://w = (d + 1 + 2 * m + 3 * (m + 1) / 5 + y + y / 4 + 5) % 7;intzeller(int y,int m,int d){if(m ==1|| m ==2){
y -=1;
m +=12;}return(d +1+2* m +3*(m +1)/5+ y + y /4- y /100+ y /400)%7;}boolcheck(){for(int i =0; i < n; i++){int year =0, month =0, day =0;for(int j =0; j <=3; j++)year = year *10+ val[s[i][j]-'A'];for(int j =5; j <=6; j++)month = month *10+ val[s[i][j]-'A'];for(int j =8; j <=9; j++)day = day *10+ val[s[i][j]-'A'];if(year <1600|| month>12|| month <1|| day>31|| day <1)return0;if((month ==4|| month ==6|| month ==9|| month ==11)&&(day >30))return0;if(isyear(year)&& month ==2&& day >29)return0;if(!isyear(year)&& month ==2&& day >28)return0;if(zeller(year, month, day)!=5)return0;}return1;}voiddfs(int now){if(flag)return;if(now ==10){if(check()){
flag =1;for(int i =0; i <10; i++)cout << val[i];
cout << endl;}return;}for(int i =0; i <10; i++){if(vis[i]==0){
vis[i]=1;
val[now]= i;dfs(now +1);
vis[i]=0;}}}intmain(){
ios::sync_with_stdio(false);int num =0;
cin >> t;while(t--){memset(vis,0,sizeof(vis));
cin >> n;for(int i =0; i < n; i++)cin >> s[i];sort(s, s + n);
n =unique(s, s + n)- s;
flag =0;
cout <<"Case #"<<++num <<": ";dfs(0);if(!flag)cout <<"Impossible"<< endl;}return0;}
最小(大)表示法最小(大)化表示代码如下#include<bits/stdc++.h>using namespace std;typedef long long ll;char s[100005];int min_show(){ int lens = strlen(s); int i = 0, j = 1, k = 0; //表示从i开始k长度和从j开始k长度的字符串相同 while (i < lens && j < lens &&