T_T越来越水了,这次只做出A+B.
A题为了代码简单直接枚举(插入位置和插入字符)
1 //CF #286 Div.2 A 2 #include<vector> 3 #include<string> 4 #include<cstdio> 5 #include<cstring> 6 #include<cstdlib> 7 #include<iostream> 8 #include<algorithm> 9 #define rep(i,n) for(int i=0;i<n;++i) 10 #define F(i,j,n) for(int i=j;i<=n;++i) 11 #define D(i,j,n) for(int i=j;i>=n;--i) 12 using namespace std; 13 const int N=10086; 14 15 void read(int &v){ 16 v=0;int sign=1; char ch=getchar(); 17 while(ch<‘0‘ || ch>‘9‘) {if (ch==‘-‘) sign=-1; ch=getchar();} 18 while(ch>=‘0‘&&ch<=‘9‘){v=v*10+ch-‘0‘; ch=getchar();} 19 v*=sign; 20 } 21 /****************tamplate***********************/ 22 23 string s,s1; 24 bool check(int pos,char ch){ 25 s1=""; 26 rep(i,pos) s1+=s[i]; 27 s1+=ch; 28 F(i,pos,s.length()-1) s1+=s[i]; 29 30 for(int l=0,r=s1.length()-1;l<r;l++,r--) 31 if (s1[l]!=s1[r]) return 0; 32 return 1; 33 } 34 35 int main(){ 36 ios::sync_with_stdio(false); 37 cin >> s; 38 int l=0,r=s.length(),pos=-1; 39 40 bool sign=1; 41 F(i,0,s.length()) 42 rep(j,26) if (check(i,j+‘a‘)) { 43 cout <<s1<<endl; 44 return 0; 45 } 46 printf("NA"); 47 return 0; 48 }
B题2B了一次,反正范围小,每种颜色的出边搜一遍就行了。
1 //CF #286 Div.2 B 2 #include<vector> 3 #include<cstdio> 4 #include<cstring> 5 #include<cstdlib> 6 #include<vector> 7 #include<iostream> 8 #include<algorithm> 9 #define rep(i,n) for(int i=0;i<n;++i) 10 #define F(i,j,n) for(int i=j;i<=n;++i) 11 #define D(i,j,n) for(int i=j;i>=n;--i) 12 #define pb push_back 13 using namespace std; 14 const int N=110; 15 16 void read(int &v){ 17 v=0;int sign=1; char ch=getchar(); 18 while(ch<‘0‘ || ch>‘9‘) {if (ch==‘-‘) sign=-1; ch=getchar();} 19 while(ch>=‘0‘&&ch<=‘9‘){v=v*10+ch-‘0‘; ch=getchar();} 20 v*=sign; 21 } 22 /****************tamplate***********************/ 23 int n,m,cnt=0; 24 struct edge{ 25 int from,to,color; 26 }; 27 vector<edge>E; 28 vector<int>G[N]; 29 void add(int x,int y,int z){ 30 E.pb((edge){x,y,z}); 31 E.pb((edge){y,x,z}); 32 int m=E.size(); 33 G[x].pb(m-2); 34 G[y].pb(m-1); 35 } 36 37 bool vis[N],yes[N]; 38 void dfs(int x,int fa,int y,int col){ 39 if (x==y) {yes[col]=1; return;} 40 vis[x]=1; 41 rep(i,G[x].size()){ 42 edge&e=E[G[x][i]]; 43 if (e.color==col && vis[e.to]==0) dfs(e.to,x,y,col); 44 } 45 } 46 47 int main(){ 48 read(n); read(m); 49 int x,y,z; 50 F(i,1,m){ 51 read(x); read(y); read(z); 52 add(x,y,z); 53 } 54 int Q; 55 read(Q); 56 F(i,1,Q){ 57 read(x); 58 read(y); 59 memset(yes,0,sizeof yes); 60 rep(i,G[x].size()){ 61 memset(vis,0,sizeof vis); 62 dfs(E[G[x][i]].to,x,y,E[G[x][i]].color); 63 } 64 int cnt=0; 65 F(i,1,m) cnt+=yes[i]; 66 printf("%d\n",cnt); 67 } 68 return 0; 69 }
时间: 2024-10-24 04:55:49