锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 2錛屽鏋滃壀鏋濓紝
Io and Ao are playing a word game. They say a word in the dictionary by turns. The word in the dictionary only contains lowercase letters. And the end character of the former said word should be the same as the start character of the current said word. They can start the game from any word in the dictionary. Any word shouldn't be said twice. Now, we define the complexity of the game that is the sum length of all words said in the game. Give you a dictionary, can you tell me the max complexity of this word game? The first line contains a single positive integer n(0 < n <=12). Next n lines are n words in the dictionary. The length of each word will not exceed 100. A single integer represents the complexity of the game.
Problem Source: provided by loon #include<iostream> //system("PAUSE"); 浠ヤ笂浠g爜瓚呮椂銆傚畬鍏ㄥ彲浠ュ壀鏋濄?br>涓句釜渚嬪瓙 //system("PAUSE"); 鍥犱負I錛?pre鍙堥敊浜嗗嚑涓嬨?br>浠ュ悗debug灝介噺鑷繁鐢ㄧ溂鐫涚湅錛屾洿鐪佹椂闂達紒錛侊紒錛侊紒錛侊紒錛侊紒
鏍規嵁鍥炴函鐨勮繃紼嬶紝浼氬厛姹傚埌dfs(n)鏈鍚庨偅涓粨鐐癸紝鍐嶆槸dfs(n-1)..dfs(1)銆?br>鍓灊搴斿綋鏄湪緇撴瀯浣撲腑N[i]澧炲姞涓涓彉閲?/span>vnow,
vnow鍒濆間負0錛岃褰曚粠璇ョ粨鐐瑰紑濮嬫悳绱㈡墍鏈?/span>int dfs(i)鐨勮繑鍥炲鹼紝騫朵笉鏂洿鏂頒嬌鍏舵渶澶э紝
褰撴悳绱㈠埌i鐨勬椂鍊欙紝濡傛灉N[i].vnow涓嶄負0錛屽垯闇瑕佹弧瓚?/span>N[i].vnow+褰撳墠temp錛堝墠闈㈢殑鍙緋誨垪鐨勬誨鹼級>vmax(鎵瑕佹眰鐨勬渶澶у肩殑褰撳墠鍊?/span>)錛岃嫢vnow涓?/span>0錛屽垯鍙互涓嶆弧瓚寵繖涓潯浠訛紝鍥犱負榪欐槸絎竴嬈°?/span>
]]> into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Input
n (0 < n <= 16)
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements.
You are to write a program that completes above process.
Sample Input
6
8
Sample Output
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4
Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
寰堢粡鍏哥殑娣辨悳錛屽惎鍙?br>1錛岄澶勭悊闈炲父閲嶈錛?br>錛?錛夛紝鍏堟眰鍑烘墍鏈夌殑prime鏁版渶澶?16+16錛宐ool isprime[32],
錛?錛夌敱浜庡凡緇忕粰鍑鴻寖鍥達紝鎴戜滑鍙互鍏堟眰鍑?-19鐨勬瘡涓暟涓嬩竴涓彲鑳界殑鍙栧鹼紝渚嬪15錛屼笅涓涓彲浠ュ彇2,4,------涓ら噸for寰幆錛屽彲浠ラ伩鍏嶆悳绱㈡椂閲嶅鍒ゆ柇銆?br>2錛岀敤涓涓暟緇勮褰曚嬌杈撳嚭鐨勬湁搴忥紝op[n]涓巇fs(int begin,int n)涓璶瀵瑰簲
if( bp[t] )
{
op[o] = t ;
o ++ ;
bp[t] = false ;
dfs( t , n );//
o -- ;
bp[t] = true ;
}
]]>
Status
In/Out
TIME Limit
MEMORY Limit
Submit Times
Solved Users
JUDGE TYPE
stdin/stdout
3s
8192K
439
74
Standard
Input
Output
Sample Input
3
joj
jlu
acm
6
cctv
redcode
lindong
we
love
programming
3
daoyuanlee
come
on
Sample Output
6
11
10
#include<cstdlib>
using namespace std;
struct S
{
string a;
char begin;
char end;
int length;
}s[13];
int visited[13];
int temp;
void search(int a,int num,int pre)
{
for(int i=0;i<num;i++ )
{
if(s[i].begin==s[pre].end&&visited[i]==0)
{
visited[i]=1;
search(a+s[i].length,num,i);
if(a+s[i].length>temp)temp=a+s[i].length;
visited[i]=0;
}
}
}
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int num;
while(cin>>num)
{
int i;
temp=0;
for( i=0;i<num;i++)
{
cin>>s[i].a;
s[i].length=(s[i].a).size();
s[i].begin=(s[i].a)[0];
s[i].end=(s[i].a)[s[i].length-1];
if(s[i].length>temp)
temp=s[i].length;
}
for(i=0;i<num;i++)
{
memset(visited,0,sizeof(visited));
visited[i]=1;
search(s[i].length,num,i);
}
cout<<temp<<endl;
}
return 0;
}
abc
cbd
dbm
dbacmdp
鎴戠殑紼嬪簭涓鐩存悳鍟婃悳錛屾瘡嬈℃悳瀹岄兘閲嶆柊寮濮嬨傛瘮濡傚湪浠寮澶村悗錛屾悳鍒癱錛屼笅嬈″啀鎼滅儲鏃剁洿鎺ュ埄鐢╟鐨勭粨鏋滐紝榪欐槸娣辨悳鐨勭壒鐐瑰喅瀹氱殑錛侊紒錛?br>*************************
榪欑綾諱技鐨勬湁搴忔悳绱㈤兘鍙互鐢?nbsp; * 澶囧繕褰曟柟娉?
**************************
#include<iostream>
#include<cstdlib>
using namespace std;
int num;
struct S
{
string a;
char begin;
char end;
int length;
}s[13];
int visited[13];
int temp;
int sum[13];
int search(int pre)//·µ»Ø´ÓpreµãÒÔºóµÄ×ܵÄÖµ
{
int j=s[pre].length,k=0;
for(int i=0;i<num;i++ )
{
if(s[i].begin==s[pre].end&&visited[i]==0&&i!=pre)//蹇呴』瑕佹湁I錛?pre
{
visited[i]=1;
k=search(i)+s[pre].length;
if(k>j)j=k;
}
else
{
if(s[i].begin==s[pre].end&&i!=pre)//蹇呴』瑕佹湁i!=pre
return sum[i]+s[pre].length;//鐩稿綋浜庡蹇樺綍錛岃屼笖鏃犻渶visited[i]=0;
}
}
sum[pre]=j;
return j;
}
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
while(cin>>num)
{
int i,j;
temp=0;
j=0;
for( i=0;i<num;i++)
{
cin>>s[i].a;
s[i].length=(s[i].a).size();
s[i].begin=(s[i].a)[0];
s[i].end=(s[i].a)[s[i].length-1];
if(s[i].length>temp)
temp=s[i].length;
}
for(i=0;i<num;i++)
{
memset(visited,0,sizeof(visited));
memset(sum,0,sizeof(sum));
visited[i]=1;
j=search(i);
if(j>temp)
temp=j;
}
cout<<temp<<endl;
}
return 0;
}
]]>
涓嬮潰鐨勪唬鐮?#8220;no”錛屽洜涓?20 19 1),(17鎺ヤ笅鏉ュ噾涓嶅嚭鏉?nbsp;)
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
int length[21];
int mark[21];
bool cmp(int x,int y)
{
return x>y;
}
int dfs(int sum,int flag,int n,int time)//´Ótime¿ªÊ¼ËÑË÷
{
if(sum==0)return 1;
if(time<n)
{
for(int k=time;k<n;k++)
{
if(mark[k]<0&&sum-length[k]>=0)
{
mark[k]=flag;
if(dfs(sum-length[k],flag,n,k+1))
return 1;
else
mark[k]=-1;
}
}
}
return 0;
}
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int num,n;
cin>>num;
while(num--)
{
int sum=0;
cin>>n;
for(int k=0;k<n;k++)
{
cin>>length[k];
mark[k]=-1;
sum+=length[k];
}
sort(length,length+n,cmp);
if(sum%4!=0||length[0]>sum/4)
cout<<"no"<<endl;
else
{
sum/=4;
if(dfs(sum,1,n,0))
{
if(dfs(sum,2,n,0))
{
if(dfs(sum,3,n,0))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
else
cout<<"no"<<endl;
}
else
cout<<"no"<<endl;
}
}
//system("PAUSE");
return 0;
}
姝g‘鐨勮В娉曞簲褰撴槸錛屽湪涓璧鋒悳绱€?br>涓嬮潰鏄竴孌佃秴鏃剁殑浠g爜
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
int length[21];
int mark[21];
int flag=0;
int target;
bool cmp(int x,int y)
{
return x>y;
}
int func(int i)
{
while(mark[i]>0)
i++;
return i;
}
void dfs(int sum,int n,int time,int level)//浠庡簭鍙蜂負time鐨勫紑濮嬫悳绱?
{
if(flag)return ;
if(sum==0&&level==3)
{
flag=1;
return;
}
if(sum==0)
{
level++;
dfs(target,n,func(0),level);
}
else if(time<n)
{
for(int k=time;k<n;k++)
{
if(k>1)
{
if(length[k]==length[k-1]&&!mark[k-1])
continue;//鏄劇劧
}
if(sum<length[n-1]) continue;//鏄劇劧
if(mark[k]<0&&sum-length[k]>=0)
{
mark[k]=1;
dfs(sum-length[k],n,func(k+1),level);
mark[k]=-1;
}
}
}
}
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int num,n;
cin>>num;
while(num--)
{
int sum=0;
cin>>n;
for(int k=0;k<n;k++)
{
cin>>length[k];
mark[k]=-1;
sum+=length[k];
}
sort(length,length+n,cmp);
if(sum%4!=0||length[0]>sum/4)
cout<<"no"<<endl;
else
{
sum/=4;
target=sum;
dfs(sum,n,0,0);
if(flag==0)
cout<<"no"<<endl;
else
cout<<"yes"<<endl;
}
}
//system("PAUSE");
return 0;
}
涓婇潰瓚呮椂灝辮秴鏃跺師鍥犳槸鍑忔灊涓嶅交搴曘俵evel鏍囪涔熶笉澶ソ
鍐沖畾鍙﹀鐐夌伓,鍏堢湅鐪嬪埆浜虹殑浠g爜
#include<iostream>
#include<algorithm>
using namespace std;
int s[21];
int v[21];
int len;
int m;
int dfs(int cur,int num,int beg,int fin)
{
int solve(int );
if(num==1)
return 1;
if(cur==len)
{
return solve(num-1);
}
for(int i=beg;i>=fin;i--)
{
if(!v[i]&&cur+s[i]<=len)
{
v[i]=1;
if(dfs(cur+s[i],num,i-1,fin))
return 1;
v[i]=0;
}
}
return 0;
}
int solve(int edge_num)
{
int i;
for(i=m;i>=1;i--)
if(!v[i])
{
v[i]=1;
if(dfs( s[i],edge_num,i-1,1))
return 1;
v[i]=0;
}
return 0;
}
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>m;
int i;
int sum=0;
for(i=1;i<=m;i++)
{
cin>>s[i];
sum+=s[i];
v[i]=0;
}
len=sum/4;
if(4*len!=sum)
{
cout<<"no"<<endl;
continue;
}
sort(s+1,s+m+1);
if(s[m]>len)
{
cout<<"no"<<endl;
continue;
}
if(solve(4))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}//鍓灊榪樺彲浠ュ仛寰楁洿濂斤紒錛侊紒
浜哄鐨剆olve鍐欏緱濂斤紒錛侊紝緇撳悎dfs.
Print a new line after each test case.
2 1 1 2 0 0 5 3 1 2 3 5 3 1 2 4 4 5 0 0 5 5 5 1 5 2 5 3 5 4 4 1 4 2 0 0 0
Case 1: The longest path from 1 has length 1, finishing at 2. Case 2: The longest path from 3 has length 4, finishing at 5. Case 3: The longest path from 5 has length 2, finishing at 1.
鎻愪氦浜?0嬈★紝AC 3嬈★紝瓚呮椂4嬈★紝wa 3嬈°?/pre>寰堟棤璇?/pre>搴旇鏄姩鎬佽鍒掓渶濂斤紝浣嗘槸鎴戜笉鏄緢鐔燂紝鐢ㄤ簡鎼滅儲銆備互涓嬫槸瓚呮椂鐨勪唬鐮?/pre>#include<iostream> #include<cstdlib> using namespace std; int path[101][101]; int mark[101]; int len[101];// int end[101]; int dfs(int start,int num)//榪斿洖浠庡綋鍓嶇偣鍑哄彂鐨勬渶澶ч暱搴? { if(mark[start]==1)return len[start]; mark[start]=1; end[start]=start; for(int i=1;i<=num;i++) { if(path[start][i]) { if(len[start]<dfs(i,num)+1) { len[start]=len[i]+1; end[start]=end[i]; } else if(len[start]==len[i]+1&&end[start]>end[i]) end[start]=end[i]; } } mark[start]=0;//榪欏彞鍧氬喅涓嶉渶瑕? return len[start]; } int main() { // freopen("s.txt","r",stdin); //freopen("key.txt","w",stdout); int j,k,turn=0; int start,num; while(cin>>num) { turn++; if(num==0)break; memset(path,0,sizeof(path)); memset(mark,0,sizeof(mark)); memset(len,0,sizeof(len)); memset(end,0,sizeof(end)); cin>>start; while(cin>>j>>k) { if(j==0)break; path[j][k]=1; } cout<<"Case "<<turn<<": The longest path from "<<start<<" has length "<<dfs(start,num)<<", finishing at "<<end[start]<<"."<<endl<<endl; } //system("PAUSE"); return 0; }
浠ヤ笅鏄痑c鐨勪唬鐮?/pre>#include<iostream> #include<cstdlib> using namespace std; int path[102][102]; int mark[102], len[102],end[102]; int dfs(int start,int num)//榪斿洖浠庡綋鍓嶇偣鍑哄彂鐨勬渶澶ч暱搴? { if(mark[start]==1)return len[start]; mark[start]=1; end[start]=start; len[start]=0; int i,t; for( i=1;i<=num;i++) { if(path[start][i]) { t=dfs(i,num)+1; if(t>len[start]) { len[start]=t; end[start]=end[i]; } else if(len[start]==t) { if(end[start]>end[i]) end[start]=end[i]; } } } return len[start]; } int main() { //freopen("s.txt","r",stdin); //freopen("key.txt","w",stdout); int j,k,turn=0; int start,num; while(cin>>num,num) { turn++; memset(path,0,sizeof(path)); memset(mark,0,sizeof(mark)); memset(len,0,sizeof(len)); memset(end,0,sizeof(end)); cin>>start; while(cin>>j>>k,j||k) { path[j][k]=1; } dfs(start,num); cout<<"Case "<<turn<<": The longest path from "<<start<<" has length "<<len[start]<<", finishing at "<<end[start]<<"."<<endl<<endl; } //system("PAUSE"); return 0; }涓嶅Θ鎵ц涓涓?/pre>5 3 1 2 3 5 3 1 2 4 4 5 0 0 鍏堟槸len[3]=0;end[3]=3;flag[3]=1;
鍐嶆墽琛宼=dfs(1)+1,杞叆dfs錛?錛夛紱len[1]=0;end[1]=1;flag[1]=1;鍐嶆墽琛?span style="COLOR: #0000ff">t=dfs(2)+1;杞叆dfs(2),len[2]=0;end[2]=2;flag[2]=1;鍐嶆墽琛宼=dfs(4)+1
杞叆dfs(4),len[4]=0;end[4]=4;flag[4]=1;鍐嶈漿鍏?span style="COLOR: red">t=dfs錛?)+1;
杞叆dfs(5),len[5]=0;end[5]=5;flag[5]=1;return(len[5]=0);鍒檛=1;t>len[4];len[4]=1;end[4]=end[5]=5;鍐嶇湅4娌′簡鍏朵粬鐩擱偦鍏冪礌銆俤fs(4)=return(len[4])=1;t=dfs(4)+1=2;len[2]=t=2;end[2]=end[4]=5;鍐嶇湅2娌′簡鍏朵粬鐩擱偦鍏冪礌錛宒fs(2)=return(len(2)=2;
鍐嶇湅t=dfs(2)+1=3;len[1]=t=3;end[1]=en[2]=5;鍐嶇湅1鏈夋病鏈夊叾浠栫浉閭誨厓绱狅紝dfs(1)=return(len(1)=3
鍐嶆墽琛宼=dfs(1)+1,len[3]=4;end[3]=end[1]=5;鍐嶇湅3鏈夋病鏈夊叾浠栫浉閭誨厓绱狅紝鏈塪fs(5),宸茬粡閬嶅巻鍒頒簡錛屾墍浠fs錛?錛塺eturn len銆?銆戙?br>娌℃湁褰卞搷銆?br>鍋囪鏀逛負
5 3 5 2 3 5 3 1 2 4 4 1 0 0 鎵ц鏃朵細璧?->1>榪欐椂鐨?緇撶偣len[1]宸茬粡姹傜殑 3>5>2>4>1len[1]宸茬煡浜?/span>
]]>
On a chess board sizes of m*n(1<=m<=5,1<=n<=5),given a start position,work out the amount of all the different paths through which the horse could return to the start position.(The position that the horse passed in one path must be different.The horse jumps in a way like "鏃?)
The input consists of several test cases.The size of the chess board m,n(row,column),and the start position v,h(vertical , horizontal) ,separated by a space.The left-up point is (0,0)
the amount of the paths in a single line
5 4 3 1
4596
瑙f瀽錛?br>椹蛋鏃ワ紝鍏釜鏂瑰悜錛?br>涓嶅厑璁告煇鏉¤礬寰勪笂閲嶅銆?br>if(path[i][j]==1)
{
if(i==v && j==h)
counts++;
//閲嶅鐨勫鏋滄槸鍑哄彂鐐癸紝榪斿洖鍊?br> return ;//鏃犺鎬庢牱錛屽彧瑕侀噸澶嶅氨榪斿洖
}
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
![]() |
stdin/stdout | 3s | 8192K | 424 | 182 | Standard |
Given a specifie d total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
The input file will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers . If n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and
will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
For each test case, first output a line containing `Sums of ', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line `NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distinct; the same sum cannot appear twice.
4 6 4 3 2 2 1 1 5 3 2 1 1 400 12 50 50 50 50 50 50 25 25 25 25 25 25 0 0
Sums of 4: 4 3+1 2+2 2+1+1 Sums of 5: NONE Sums of 400: 50+50+50+50+50+50+25+25+25+25 50+50+50+50+50+25+25+25+25+25+25
闅劇偣錛?錛屾湁搴忛掑噺錛岃繖涓ソ澶勭悊錛屽彧闇鍦ㄥ悜鍓嶆帹鏃禿fs(i)for(int j=i+1...)鍗沖彲
Description
1033The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
1733
3733
3739
3779
8779
8179
Input
Output
Sample Input
3 1033 8179 1373 8017 1033 1033
Sample Output
6 7 0
There are multiple test cases, the first line of each test case is a integer n. (2<=n<=20) The next line have n integers (every integer in the range [1,1000]). We can divide these n integers into two parts and get the sum of the integers in these two parts, define them as sumA and sumB. Please calc the max number of sumA*sumB and output it in a single line.
3 5 3 7 2 4 9
56 36
(5,3),(7) sumA=8 sumB=7 sumA*sumB=56 (5),(3,7) sumA=5 sumB=10 sumA*sumB=50 (3),(5,7) sumA=3 sumB=12 sumA*sumB=36 (),(3,5,7) sumA=0 sumB=15 sumA*sumB=0 The max is 56.