usaco5.5-Hidden Passwords

最小表示法,感觉可以做成个模板,第一次RE是因为字符串长度变2倍了而我把数组开小了

Executing...
   Test 1: TEST OK [0.008 secs, 3760 KB]
   Test 2: TEST OK [0.005 secs, 3760 KB]
   Test 3: TEST OK [0.005 secs, 3760 KB]
   Test 4: TEST OK [0.008 secs, 3760 KB]
   Test 5: TEST OK [0.005 secs, 3760 KB]
   Test 6: TEST OK [0.003 secs, 3760 KB]
   Test 7: TEST OK [0.008 secs, 3760 KB]
   Test 8: TEST OK [0.008 secs, 3760 KB]
   Test 9: TEST OK [0.024 secs, 3760 KB]
   Test 10: TEST OK [0.014 secs, 3760 KB]
   Test 11: TEST OK [0.019 secs, 3760 KB]
   Test 12: TEST OK [0.005 secs, 3760 KB]
   Test 13: TEST OK [0.003 secs, 3760 KB]
   Test 14: TEST OK [0.008 secs, 3760 KB]

All tests OK.

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5
 6 char getch2()
 7 {
 8     char c;
 9     while(c=getchar(),c==‘\n‘);
10     return c;
11 }
12
13 char a[200010];
14 char strTmp[200010];
15 int main()
16 {
17     freopen("hidden.in","r",stdin);
18     freopen("hidden.out","w",stdout);
19     int n;
20     cin>>n;
21
22     for(int i=0;i<n;i++)
23     {
24         a[i]=getch2();
25     }
26
27     a[n]=‘\0‘;
28
29
30     strcpy(strTmp,a);
31     strcat(a,strTmp);
32
33     int i=0,j=1;
34     while(j<n)
35     {
36         int k;
37         for(k=0;k<n;k++)
38         {
39             if(a[i+k]!=a[j+k])
40                 break;
41         }
42         if(k==n)
43             break;
44         if(a[i+k]>a[j+k])
45             i+=k+1;
46         if(a[i+k]<a[j+k])
47             j+=k+1;
48
49         if(i==j)
50             j++;
51     }
52
53     cout<<i<<endl;
54     return 0;
55 }
时间: 2024-11-05 04:51:44

usaco5.5-Hidden Passwords的相关文章

[USACO5.5]Hidden Password

题目大意: 求字符串最小表示. 思路: 本来按照lbn187的课件,知道SAM可以求字符串最小表示. 然而他并没有提供例题,就自己找了一道做. 大体思想就是把字符串复制一遍接在后面,构建SAM,然后每次跑小的转移. 跑n次以后就跑到了最小表示的末尾,用该状态的len值减去n就是最小表示的起始位置. 然后交上去就MLE了. 看了网上的题解发现求最小表示有专门的做法,也是O(n)的,还特别简单,不知道比SAM妙到哪里去了. 核心思想就是设两个指针i和j,表示目前比较的循环串的开头位置. 再用k表示目

洛谷P1709 [USACO5.5]隐藏口令Hidden Password

P1709 [USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并顺时针依次取字母而组成一个字符串.这样将得到一些字符串,他把它们排序后取出第一个字符串.把这个字符串的第一个字母在原字符串中的位置-1做为口令. 如字符串alabala,按操作的到7个字符串,排序后得: aalabal abalaal a

P1709 [USACO5.5]隐藏口令Hidden Password

P1709 [USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并顺时针依次取字母而组成一个字符串.这样将得到一些字符串,他把它们排序后取出第一个字符串.把这个字符串的第一个字母在原字符串中的位置-1做为口令. 如字符串alabala,按操作的到7个字符串,排序后得: aalabal abalaal a

[USACO5.5]隐藏口令Hidden Password

题目链接:传送门 题目大意:给你一个长度 N 的字符串,5<=N<=5,000,000,将首尾合并成环,断环成链并满足字典序最小,输出此时首字母在原串中的位置-1: 题目思路:最小表示法 #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <cstring> #incl

zoj 1729 Hidden Password

Hidden Passwordhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=729 Time Limit: 2 Seconds      Memory Limit: 65536 KB Some time the programmers have very strange ways to hide their passwords. See for example how Billy "Hacker" Geits hi

[最小表示] zoj 1729 Hidden Password

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=729 Hidden Password Time Limit: 2 Seconds      Memory Limit: 65536 KB Some time the programmers have very strange ways to hide their passwords. See for example how Billy "Hacker" G

PHP Backdoors: Hidden With Clever Use of Extract Function

PHP Backdoors: Hidden With Clever Use of Extract Function February 17, 2014Daniel Cid12 Comments When a site gets compromised, one thing we know for sure is that attackers love to leave malware that allows them access back into the site; this type of

hidden node and exposed node problem

Exposed node problem In wireless networks, theexposed node problem occurs when a node is prevented from sending packets to other nodes because of a neighboring transmitter. Consider an example of 4 nodes labeled R1, S1, S2, and R2, where the two rece

Sentiment Analysis(1)-Dependency Tree-based Sentiment Classification using CRFs with Hidden Variables

The content is from this paper: Dependency Tree-based Sentiment Classification using CRFs with Hidden Variables, by Tetsuji Nakagawa. A typical approach for sentiment classification is to use supervised machine learning algorithms with bag-of-words a