I'm making a cryptographer and as a step trying to turn the string into char to be able to replace each char with a "crypto char". However I can't get this code to work and I have no idea what the problem is.
My code:
String encrypt(String text) {
int strLength = text.length();
for (int k=0, k < strLength, k++) {
letter = text.charAt(k);
}
}
I'm getting these errors:
Syntax error, insert "; ; ) Statement" to complete ForStatement
Duplicate local variable k
Syntax error on token(s), misplaced construct(s)
k cannot be resolved to a variable
Syntax error on token ")", ; expected
k cannot be resolved to a variable
at inl1.Cryptographer.encrypt(Cryptographer.java:25)
Line 25 is the "for" line.
Help is very appreciated!
I don't speek Java, but I think your for cycle should use semicolons ; instead of commas , :-)
for (int k=0; k < strLength; k++) {
...
}
Related
I was solving
https://www.spoj.com/problems/BEADS/
above question at SPOJ. I have stated the relevant information below:
Problem Statement:
The description of the necklace is a string A = a1a2 ... am specifying sizes of the particular beads, where the last character am is considered to precede character a1 in circular fashion.
The disjoint point i is said to be worse than the disjoint point j if and only if the string aiai+1 ... ana1 ... ai-1 is lexicografically smaller than the string ajaj+1 ... ana1 ... aj-1. String a1a2 ... an is lexicografically smaller than the string b1b2 ... bn if and only if there exists an integer i, i <= n, so that aj=bj, for each j, 1 <= j < i and ai < bi.
Output:
For each test case, print exactly one line containing only one integer -- number of the bead which is the first at the worst possible disjoining, i.e. such i, that the string A[i] is lexicographically smallest among all the n possible disjoinings of a necklace. If there are more than one solution, print the one with the lowest i.
Now the solution is using SUFFIX ARRAY. Input string s, and concat with itself, s'=s+s ,since I have to sort cyclic suffixes of array. Then create a suffix array on s', and output the smallest index that points to a suffix of original s, i.e., index < len(s).
But there is a problem I face. I was appending '$' character to get SA, but I was getting wrong answer. After looking online, I found 1 solution that had appended '}' to string.
I found that ascii('$') < ascii('a') < ascii('z') < ascii('}')
But i don't understand how this will make a difference, why this is accepted answer and haven;t found a case where this will make a difference. The solution (AC) can be found here:
Link to Code
#include <bits/stdc++.h>
using namespace std;
string s;int n;
bool cmp_init(int a, int b)
{
return s[a]<s[b] || (s[a]==s[b] && a<b);
}
int jmp;
vector<int> pos;
bool cmp(int a, int b)
{
return pos[a]<pos[b] || (pos[a]==pos[b] && pos[(a+jmp)%n]<pos[(b+jmp)%n]);
}
int main() {
int tc;cin>>tc;
while(tc--){
cin>>s;
int m=s.size();
s=s+s+"{";
n=s.size();
vector<int> SA(n,0);
for(int i=0;i<n;i++)SA[i]=i;
sort(SA.begin(), SA.end(), cmp_init);
pos.assign(n,0);
for(int i=1 , c=0;i<n;i++)pos[SA[i]]=(s[SA[i]]==s[SA[i-1]])?c:++c;
for(jmp=1;jmp<=n;jmp*=2)
{
sort(SA.begin(), SA.end(), cmp);
vector<int> tmp(n,0);
for(int i=1 , c=0;i<n;i++)tmp[SA[i]]=(pos[SA[i]]==pos[SA[i-1]] && pos[(SA[i]+jmp)%n]==pos[(SA[i-1]+jmp)%n])?c:++c;
for(int i=0;i<n;i++)pos[i]=tmp[i];
}
for(int i=0;i<n;i++)if(SA[i]<m){cout<<SA[i]+1<<"\n";break;}
}
}
PS.: I have found that SA construction code is correct, only problem is with last character appending. Normally we append '$' in SA construction.
The difference is in the last condition:
If there are more than one solution, print the one with the lowest i.
Consider input "abab".
The correct answer is 0, which you get when you append '}', because "abababab}" is less than all of its suffixes.
If you append '$', you get the wrong answer, because "ab$" < "abab$" < "ababab$" < "abababab$".
The problem consists of finding all permutations using k out of n digits. I'm able to find all the permutations, but I'm struggling trying to erase duplicates. I can successfully compare and find the duplicates, but erasing them is what I'm struggling to do. I have a feeling I'm missing something simple but I don't know what it is.
Any help would be greatly appreciated. I've been staring at this for a week.
Here is the code that I've got right now.
void getPermutations(int n, int k)
{
string str = "";
//fill string with numbers <= n
for(int i = 0; i < n; i++)
{
str += to_string(i); //convert numbers to string
}
string tempStr = "";
string outputStr = "";
do {
tempStr = str.substr(0, k);
int compareResult = tempStr.compare(0, k, outputStr, 0, k);
if (compareResult == 0)
{
cout << "| same | ";
outputStr.erase(k,k);
}
outputStr = tempStr;
cout << outputStr << " ";
} while (next_permutation(str.begin(), str.end()));
}
I think what you meant to do was to erase the contents of tempStr, not outputStr.
The call to erase is not exactly right. Its first argument marks the starting position of your erasing, and the second argument tells how many characters to erase. So if you want to erase the whole string, the first argument should be...
You actually don't have to erase anything. After you get it working your way, try to do it without erasing!
Good Luck!
I am using following code to get string from skip function. But i am getting integer numbers. I will appreciate if someone can help me out.
int csvToSkip(string csv, Skip skip, char delimeter)
{
int i = 0
int j = 0
int index = 0
for (i = 0; i < length(csv); i++)
{
if (csv[i] == delimeter)
{
put(skip, 0, "1")
j = i + 1
}
else if (i == length(csv) - 1)
{
put(skip, 1, "2")
}
}
return(index)
}
Skip mySkip=create;
string test="hi this is test;for another test";
char delimiter =';';
int x=csvToSkip(test, mySkip, delimiter );
print x;
for sValue in mySkip do
{
print (int key mySkip) " " sValue "\n";
}
This gives me following result
0
0 204534013
1 204534015
You did not declare sValue, so DXL guessed wrongly what data type the values have.
The first chapter of DXL Manual -> Language fundamentals, called "Auto-Declare", explains how you can disable the auto-declare functionality. If you do this, DOORS will warn you when you access undeclared variables.
given a string that includes unbalanced brackets of some order, how can I find the first index that is the unbalancing factor of my string and return it?
for example: if the string is: ")" the return value will be 0 ,
if the string is: "(((jjkk))))" the return value will be 10 ,
if the string is: "((" the return value will be 2 ,
I have implemented a function that returns boolean regarding if my string is balanced or not. (T - balanced)
I know it should be some recursive function but just can't figure it out...
Thanks!
A nonrecursive solution (written in pseudosomething loosely resembling C#) ...
string input_str = "this(is(for)(testing))(the)correctness)(of(brackets))";
int left_brackets_cnt = 0;
for (int i=1; i<=input_str.length; i++)
{
switch (input_str.char_at_position(i))
{
case "(": left_brackets_cnt++;
case ")": left_brackets_cnt--;
}
if (left_brackets_cnt < 0)
throw exception("Unmatched right bracket at position "+i);
}
if (left_brackets_cnt > 0)
throw exception("Too many left brackets");
Footnote: Please show appreciation - I typed this on a 3" smartphone! :-)
I asked this question in a few interviews. I want to know from the Stackoverflow readers as to what should be the answer to this question.
Such a seemingly simple question, but has been interpreted quite a few different ways.
if your definition of a "word" is a series of non-whitespace characters surrounded by a whitespace character, then in 5 second pseudocode you do:
var words = split(inputString, " ")
var reverse = new array
var count = words.count -1
var i = 0
while count != 0
reverse[i] = words[count]
count--
i++
return reverse
If you want to take into consideration also spaces, you can do it like that:
string word = "hello my name is";
string result="";
int k=word.size();
for (int j=word.size()-1; j>=0; j--)
{
while(word[j]!= ' ' && j>=0)
j--;
int end=k;
k=j+1;
int count=0;
if (j>=0)
{
int temp=j;
while (word[temp]==' '){
count++;
temp--;
}
j-=count;
}
else j=j+1;
result+=word.substr(k,end-k);
k-=count;
while(count!=0)
{
result+=' ';
count--;
}
}
It will print out for you "is name my hello"
Taken from something called "Hacking a Google Interview" that was somewhere on my computer ... don't know from where I got it but I remember I saw this exact question inside ... here is the answer:
Reverse the string by swapping the
first character with the last
character, the second with the
second-to-last character, and so on.
Then, go through the string looking
for spaces, so that you find where
each of the words is. Reverse each of
the words you encounter by again
swapping the first character with the
last character, the second character
with the second-to-last character, and
so on.
This came up in LessThanDot Programmer Puzzles
#include<stdio.h>
void reverse_word(char *,int,int);
int main()
{
char s[80],temp;
int l,i,k;
int lower,upper;
printf("Enter the ssentence\n");
gets(s);
l=strlen(s);
printf("%d\n",l);
k=l;
for(i=0;i<l;i++)
{
if(k<=i)
{temp=s[i];
s[i]=s[l-1-i];
s[l-1-i]=temp;}
k--;
}
printf("%s\n",s);
lower=0;
upper=0;
for(i=0;;i++)
{
if(s[i]==' '||s[i]=='\0')
{upper=i-1;
reverse_word(s,lower,upper);
lower=i+1;
}
if(s[i]=='\0')
break;
}
printf("%s",s);
return 0;
}
void reverse_word(char *s,int lower,int upper)
{
char temp;
//int i;
while(upper>lower)
{
temp=s[lower];
s[lower]=s[upper];
s[upper]=temp;
upper=upper-1;
lower=lower+1;
}
}
The following code (C++) will convert a string this is a test to test a is this:
string reverseWords(string str)
{
string result = "";
vector<string> strs;
stringstream S(str);
string s;
while (S>>s)
strs.push_back(s);
reverse(strs.begin(), strs.end());
if (strs.size() > 0)
result = strs[0];
for(int i=1; i<strs.size(); i++)
result += " " + strs[i];
return result;
}
PS: it's actually a google code jam question, more info can be found here.