A题:
给出一个长度为n的字符串,操作:在这个字符串的任意位置(前后也可以)插入一个字符,使之成为一个回文串,
而且,即使这个字符串本身就是回文串,你也必须插入一个字符。
若无法达成目的,输出"NA"
由于题意n<=10,所以直接暴力。
1.看本身是否是回文串
2.若本身不是,则暴力:
从前到后每次去掉这个字符串的一个字符,看新形成的字符串是否为回文串,若是则在这个去掉的字符的相应位置插入这个字符,还是回文串,达成目的。
若不是,则无法达成目的,输出“NA”
我这段代码的问题:s下标从0开始,新形成的字符串下标从1开始,导致很紊乱。本来想改的,好困啊,睡觉了。
1 #include<cstdio> 2 #include<cstring> 3 char s[11]; 4 int main() 5 { 6 while(scanf("%s",&s)!=EOF){ 7 int len=strlen(s); 8 bool cnt=true; 9 for(int i=1;i<=len/2;i++) 10 if(s[i-1]!=s[len-i]){ 11 cnt=false; 12 break; 13 } 14 if(cnt){ 15 for(int i=1;i<=len/2;i++) 16 printf("%c",s[i-1]); 17 printf("%c",s[len/2]); 18 for(int i=len/2+1;i<=len;i++) 19 printf("%c",s[i-1]); 20 printf("\n"); 21 continue; 22 } 23 for(int i=1;i<=len;i++){ 24 char t[11]; 25 int j=0,k=1; 26 while(j<len){ 27 if(j==i-1) 28 j++; 29 else{ 30 t[k++]=s[j]; 31 j++; 32 } 33 } 34 bool flag=true; 35 for(int l=1;l<k;l++){ 36 if(t[l]!=t[k-l]){ 37 flag=false; 38 break; 39 } 40 } 41 if(flag){ 42 i--; 43 if(i<=len/2){ 44 for(int l=0;l<len-i;l++) 45 printf("%c",s[l]); 46 printf("%c",s[i]); 47 for(int l=len-i;l<len;l++) 48 printf("%c",s[l]); 49 printf("\n"); 50 goto loop; 51 } 52 else{ 53 for(int l=0;l<len-i-1;l++) 54 printf("%c",s[l]); 55 printf("%c",s[i]); 56 for(int l=len-i-1;l<len;l++) 57 printf("%c",s[l]); 58 printf("\n"); 59 goto loop; 60 } 61 } 62 } 63 printf("NA\n"); 64 loop: ; 65 } 66 return 0; 67 }
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 ton. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.
Mr. Kitayuta wants you to process the following q queries.
In the i-th query, he gives you two integers — ui and vi.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.
The first line of the input contains space-separated two integers — n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), denoting the number of the vertices and the number of the edges, respectively.
The next m lines contain space-separated three integers — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj).
The next line contains a integer — q (1 ≤ q ≤ 100), denoting the number of the queries.
Then follows q lines, containing space-separated two integers — ui and vi (1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.
For each query, print the answer in a separate line.
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
2
1
0
5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4
1
1
1
1
2
1 #include<cstdio> 2 #include<cstring> 3 4 const int maxn=210; 5 struct Edge 6 { 7 int to,c,next; 8 }edge[maxn*2]; 9 int head[maxn],tot; 10 11 void head_init() 12 { 13 tot=0; 14 memset(head,-1,sizeof(head)); 15 } 16 void addedge(int u,int v,int c) 17 { 18 edge[tot].c=c; 19 edge[tot].to=v; 20 edge[tot].next=head[u]; 21 head[u]=tot++; 22 } 23 24 bool color[maxn],flag; 25 bool vis[maxn]; 26 void dfs(int cur,int col,int point) 27 { 28 if(cur==point) 29 { 30 flag=true; 31 return ; 32 } 33 if(flag) 34 return ; 35 vis[cur]=true; 36 for(int i=head[cur];i!=-1;i=edge[i].next) 37 { 38 int c=edge[i].c; 39 int v=edge[i].to; 40 if(c!=col) 41 continue; 42 if(vis[v]) 43 continue; 44 dfs(v,col,point); 45 vis[v]=false; //回溯 46 } 47 } 48 49 int main() 50 { 51 int n,m; 52 while(scanf("%d%d",&n,&m)!=EOF) 53 { 54 head_init(); //初始化 55 memset(color,false,sizeof(color)); 56 memset(vis,false,sizeof(vis)); 57 int u,v,c; 58 for(int i=1;i<=m;i++) 59 { 60 scanf("%d%d%d",&u,&v,&c); 61 addedge(u,v,c); 62 addedge(v,u,c); //无向边 63 color[c]=true; //标记有这种颜色 64 } 65 int q; 66 scanf("%d",&q); 67 for(int i=1;i<=q;i++) 68 { 69 scanf("%d%d",&u,&v); 70 int sum=0; 71 for(int i=1;i<=m;i++) 72 if(color[i]) 73 { 74 flag=false; //能不能到达的标记 75 dfs(u,i,v); 76 vis[u]=false; //回溯 77 if(flag) 78 sum++; 79 } 80 printf("%d\n",sum); 81 } 82 } 83 return 0; 84 }