程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu-----(3746)Cyclic Nacklace(kmp),3746nacklace

hdu-----(3746)Cyclic Nacklace(kmp),3746nacklace

編輯:C++入門知識

hdu-----(3746)Cyclic Nacklace(kmp),3746nacklace


Cyclic Nacklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2716    Accepted Submission(s): 1244


Problem Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task.

As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl's fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls' lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet's cycle is 9 and its cyclic count is 2:

Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden.
CC is satisfied with his ideas and ask you for help.  

 

Input The first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases.
Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by 'a' ~'z' characters. The length of the string Len: ( 3 <= Len <= 100000 ).  

 

Output For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.  

 

Sample Input 3 aaa abca abcde  

 

Sample Output 0 2 5  

 

Author possessor WC  

 

Source HDU 3rd “Vegetable-Birds Cup” Programming Open Contest  

 

Recommend   代碼: 1 /* 2 類是於這樣的題,其實關鍵還是再考察對next數組的理解 3 next數組就是一種關於字符串的前綴數組。 4 同時需要明白的 是: j-next[j]=len表示的是他的回溯的最大長度 5 也可以理解為:最小的循環長度。 6 所以在做這題是,我們需要求出最少需要補償多少個字符,是她形成 7 完美的字符串: abcdeab -->next[0~~6]= -1,0,0,0,0,1,2 8 所以我們可以得到: max_huisuo=7-next[7]=5; 9 所以我們需要補償的數為: mx_huisuo -7%5=3; //比如abcdeab-->cde 10 */ 11 12 #include<iostream> 13 #include<cstring> 14 #include<cstdlib> 15 #include<cstdlib> 16 using namespace std; 17 const int maxn=100050; 18 int next[maxn]; 19 char str[maxn]; 20 int main() 21 { 22 int test,i,j,ans; 23 scanf("%d",&test); 24 while(test--) 25 { 26 scanf("%s",str); 27 j=-1; 28 i=0; 29 next[i]=-1; 30 int len=strlen(str); 31 while(i<len) 32 { 33 if(j==-1||str[i]==str[j]) 34 { 35 i++; 36 j++; 37 if(str[i]==str[j]) 38 next[i]=next[j]; 39 else next[i]=j; 40 } 41 else j=next[j]; 42 } 43 //得到最大回縮長度(即最小循環長度; 44 int cir_len=len-next[len]; 45 if(cir_len!=len&&0==(len%cir_len)) ans=0; 46 else ans=cir_len - len%cir_len ; 47 printf("%d\n",ans); 48 } 49 return 0; 50 } View Code

 


ACM hdu 1711(KMP),可以出來結果,怎就AC不了?

#include <iostream>

using namespace std;

int m,n,str1[1000005],str2[10005],next[10005];

void getnext(int str2[])
{
int i=0,j=-1;
next[0]=-1;
while(i<m-1)
{
if(j==-1 || str2[i]==str2[j])
{
i++;
j++;
if(str2[i]!=str2[j])
next[i]=j;
else
next[i]=next[j];
}
else j=next[j];
}

}

int kmp(int str1[], int str2[],int pos)
{
int i=pos,j=0;
while(i<n&&j<m)
{
if(j==-1 || str1[i]==str2[j])
{
i++;
j++;
}
else
j=next[j];
}
if(j>=m)
return i-m+1;
else
return -1;

}

int main()
{
int t,i;
cin>>t;
while(t--)
{
//if(t==0) 你這樣會miss最後一組輸入的
//return 0;
cin>>n>>m;
for(i=0;i<n;i++)
cin>>str1[i];
for(i=0;i<m;i++)
cin>>str2[i];
getnext(str2);

cout<<kmp(str1,str2,0)<<endl;
}
return 0;
}
 

算法設計比賽做什算法好

應該是ACM吧
就是給你8-10道算法題目,5個小時,做出來多的題目數越多,排名越靠前,如果題目數一樣多的看用的時間。
時間的計算方法如下:
例如你A題用了20分鐘AC,然後B題有用了30分鐘AC(此時是比賽開始50分鐘),又用了30分鐘AC了C題,那麼你的時間(penalty )是
20 + 50 + 80 = 150分鐘

比賽中常用的算法有
1。動態規劃
2。搜索
3。貪心
4。圖論
5。組合數學
6。計算幾何
7。數論


推薦到
acm.pku.edu.cn
acm.zju.edu.cn
acm.hdu.edu.cn
acm.timus.ru
這幾個OJ上練習

比較好的題目分類(POJ上的)
1。這個是我最喜歡的
初期:
一.基本算法:
(1)枚舉. (poj1753,poj2965)(2008-10-27Done 位運算+寬搜)
(2)貪心(poj1328,poj2109,poj2586)
(3)遞歸和分治法.
(4)遞推.
(5)構造法.(poj3295)
(6)模擬法.(poj1068,poj2632,poj1573,poj2993,poj2996)
二.圖算法:
(1)圖的深度優先遍歷和廣度優先遍歷.
(2)最短路徑算法(dijkstra,bellman-ford,floyd,heap+dijkstra)(2008-08-29Done)
(poj1860,poj3259,poj1062,poj2253,poj1125,poj2240)
(3)最小生成樹算法(prim,kruskal)
(poj1789,poj2485,poj1258,poj3026)
(4)拓撲排序 (poj1094)(2008-09-01Done)
(5)二分圖的最大匹配 (匈牙利算法) (poj3041,poj3020)
(6)最大流的增廣路算法(KM算法). (poj1459,poj3436)
三.數據結構.
(1)串 (poj1035,poj3080,poj1936)
(2)排序(快排、歸並排(與逆序數有關)、堆排) (poj2388,poj2299)
(3)簡單並查集的應用.
(4)哈希表和二分查找等高效查找法(數的Hash,串的Hash)
(poj3349,poj3274,POJ2151,poj1840,poj2002,poj2503)
(5)哈夫曼樹(poj3253)(2008-09-02Done)
(6)堆
(7)trie樹(靜態建樹、動態建樹) (poj2513)(2008-10-23Done 並查集、歐拉)
四.簡單搜索
(1)深度優先搜索 (poj2488,poj3083,poj3009,poj1321,poj2251)
(2)廣度優先搜索(poj3278,poj1426,......余下全文>>
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved